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:
    以表达的形式定义触发条件
  • 相关阅读:
    ubuntu安装打印机驱动
    clang-format的介绍和使用
    [C++面向对象]-C++成员函数和非成员函数
    [Qt2D绘图]-06QPainter的复合模式&&双缓冲绘图&&绘图中的其他问题
    [Qt2D绘图]-05绘图设备-QPixmap&&QBitmap&&QImage&&QPicture
    [Qt2D绘图]-04绘制文字&&绘制路径
    [Qt2D绘图]-03坐标系统之坐标变换
    [Qt2D绘图]-02坐标系统&&抗锯齿渲染
    [Qt2D绘图]-01Qt2D绘图基本绘制和填充
    [Qt插件]-01Qt插件&&提升部件(自定义控件)
  • 原文地址:https://www.cnblogs.com/haosit/p/8315702.html
Copyright © 2011-2022 走看看