zoukankan      html  css  js  c++  java
  • C# 使用Quartz简单实例以及备忘

    一、导入NuGet

     二、创建一个类并实现接口Ijob,并实现该接口中的方法。

    using Buday.Gold.Cusumer.Lib;
    using Quartz;
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Quartz.CheckoutService
    {
        public class JobInterest : IJob
        {
            public Task Execute(IJobExecutionContext context)
            {
                return Task.Run(() =>
                {
                    //在此处实现作业的业务逻辑
                    Console.WriteLine("");
                   
    
                });
            }
            
        }
    }

    三、使用

    using Quartz;
    using Quartz.Impl;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Buday.Gold.Cusumer.CheckoutService
    {
        class Program
        {
            static void Main(string[] args)
            {
                var scheduler = StdSchedulerFactory.GetDefaultScheduler().GetAwaiter().GetResult();
                //创建触发条件
                ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("InterestTrigger", "Interest")
                .StartNow()
                .WithDailyTimeIntervalSchedule(t =>
                {
                    t.OnEveryDay();//每天都执行
                    t.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 30));//设置执行的开始时间
                    //只设置开始时间,会在开始以后一直执行
                    t.EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(1, 0));//设置停止执行的时间
                    //二者表示,开某个时间段执行
                    t.WithIntervalInHours(2);//执行的间隔
                })
                .Build();
                //创建作业内容
                IJobDetail job = JobBuilder.Create(typeof(JobInterest))
               .WithIdentity("InterestJob", "Interest")
               .Build();
                scheduler.ScheduleJob(job, trigger);
                scheduler.Start();
            }
        }
    }
    四、收集的一些 ITrigger 属性说明:

    1.WithSimpleSchedule:
    指定从某一个时间开始,以一定的时间间隔(单位是毫秒)执行的任务。
    .WithSimpleSchedule(t => {
          t.RepeatForever();//重复次数不限
          //上下两者取其一
          t.WithRepeatCount(5);//设置重复次数,例如5次
          t.WithIntervalInHours(1);//设置执行间隔
          //上下两者取其一
          t.WithInterval(new TimeSpan(1, 2, 10));//设置重复间隔,用具体的小时,分钟,秒
     })
    2.WithCalendarIntervalSchedule:
    和WithSimpleSchedule类似,不同的是.SimpleSchedule指定的重复间隔只有(时,分,秒)而CalendarIntervalSchedule可以时(年,月,周,天,时,分,秒)
    .WithCalendarIntervalSchedule(t => {
          t.WithIntervalInDays(1);//间隔以天为单位
          t.WithIntervalInWeeks(1);//间隔以周为单位
          t.WithIntervalInMonths(1);//间隔以月为单位
          t.WithIntervalInYears(1);//间隔以年为单位
    })
    3.WithDailyTimeIntervalSchedule:
    指定每天的某个时间段内,以一定的时间间隔执行任务。并且它可以支持指定星期
    .WithDailyTimeIntervalSchedule(t => {
          t.OnEveryDay();//每天执行
          t.OnDaysOfTheWeek(DayOfWeek.Monday,DayOfWeek.Saturday);//每周的星期几执行
          t.OnMondayThroughFriday();//设置工作日执行(周一至周五)
          t.OnSaturdayAndSunday();//设置周末执行
          t.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0,30));//设置执行的开始时间
          //只设置开始时间,会在开始以后一直执行
          t.EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(1, 0));//设置停止执行的时间
          //二者表示,开某个时间段执行
          t.WithIntervalInHours(2);//设置重复间隔(更据方法不同可是时,分,秒)
          t.WithRepeatCount(10);//设置总共执行次数
    })
    4.StartNow()和.StartAt(new DateTimeOffset(new DateTime(2018,1,10))):
    StartNow:表示启动后立即执行一次.
    StartAt:表示启动后在指定日期或时间开始执行
    5.WithCronTrigger:
    以表达的形式定义触发条件
  • 相关阅读:
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之六 多点触控
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之九 定位
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之七 重力感应
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之五 保存数据的几种方式
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之八 照相机
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之三 Application 配置详解
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之四 打开与关闭应用程序是的保存数据
    ADOBE FLASH BUILDER 4.6 IOS 开发之部署与调试
    [译] 高性能JavaScript 1至5章总结
    页签及盒子的web标准实现
  • 原文地址:https://www.cnblogs.com/haosit/p/8315702.html
Copyright © 2011-2022 走看看