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:
以表达的形式定义触发条件

转载于:https://www.cnblogs.com/haosit/p/8315702.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值