Quartz.NET学习系列(七)--- 日历式任务

本文介绍 Quartz.NET 中如何使用 AnnualCalendar 类实现特定日期的任务排除,通过示例展示了如何创建并应用日历,以避免在指定的节假日执行任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        Quartz.NET除了支持前面几篇讲的持续时间式的任务外,还支持日历式的任务,日历式的任务主要应用场景比如一些节假日或者特定纪念日的任务执行或排除。主要通过

AnnualCalendar 类来控制。

代码如下:

        public class SimpleJob : IJob
	{
		private static readonly ILog log = LogManager.GetLogger(typeof(SimpleJob));
		
		public virtual void Execute(IJobExecutionContext context)
		{
			JobKey jobKey = context.JobDetail.Key;
			log.InfoFormat(" {0} 执行时间 {1}", jobKey, System.DateTime.Now.ToString());
		}
	}


    public class CalendarExample
    {

        public static void Run()
        {
            ILog log = LogManager.GetLogger(typeof (CalendarExample));

            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler sched = sf.GetScheduler();

            AnnualCalendar holidays = new AnnualCalendar();

            DateTime fourthOfJuly = new DateTime(DateTime.UtcNow.Year, 3, 15);
            holidays.SetDayExcluded(fourthOfJuly, true);

            DateTime halloween = new DateTime(DateTime.UtcNow.Year, 10, 31);
            holidays.SetDayExcluded(halloween, true);

            DateTime christmas = new DateTime(DateTime.UtcNow.Year, 12, 25);
            holidays.SetDayExcluded(christmas, true);

            sched.AddCalendar("holidays", holidays, false, false);

            IJobDetail job = JobBuilder.Create<SimpleJob>()
                .WithIdentity("job1", "group1")
                .Build();

            ISimpleTrigger trigger = (ISimpleTrigger) TriggerBuilder.Create()
                                                          .WithIdentity("trigger1", "group1")
                                                          .StartNow()
                                                          .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever())
                                                          .ModifiedByCalendar("holidays")
                                                          .Build();

            DateTimeOffset ft = sched.ScheduleJob(job, trigger);

            log.Info(string.Format("{0} 开始时间 {1} 重复次数 {2}, 间隔时间 {3}", job.Key, ft.ToString(), trigger.RepeatCount, trigger.RepeatInterval.TotalSeconds));

            sched.Start();
            log.Info("------- 开始计划 ----------------");
            Thread.Sleep(30*1000);
            sched.Shutdown(true);
            log.Info("------- 关闭计划 -----------------");

            SchedulerMetaData metaData = sched.GetMetaData();
            log.Info(string.Format("执行次数{0}", metaData.NumberOfJobsExecuted));
        }
    }

其中

holidays.SetDayExcluded(fourthOfJuly, true);
表示排除该日期,如果第二个参数传false表示包括该日期。

ISimpleTrigger trigger = (ISimpleTrigger) TriggerBuilder.Create()
                                                          .WithIdentity("trigger1", "group1")
                                                          .StartNow()
                                                          .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever())
                                                          .ModifiedByCalendar("holidays")
                                                          .Build();

ModifiedByCalendar("holidays")表示应用这个日历的一些设置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值