Quartz.Net系列(十二):六大Calendar(Annual、Cron、Daily、Holiday、Monthly、Weekly)

IT技術派發表於2020-07-12

Quartz.Net中為了動態排除一些時間,而使用Calendar可以做到

1.DailyCalendar

可以動態的排除一天中的某些時間段

示例:在一天當中的13:00到14:00不要執行

        public async static Task Test()
        {
            var scheduler = await StdSchedulerFactory.GetDefaultScheduler();

            await scheduler.Start();

            DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(13,0,0).DateTime,
                                                            DateBuilder.DateOf(14,0,0).DateTime);

            var job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();

            var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                                               d => d.OnEveryDay()
                                                     .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                                                     .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 00))
                                                     .WithIntervalInSeconds(1)
                                              )
                                               .ModifiedByCalendar("myCalendar")
                                               .Build();

            await scheduler.AddCalendar("myCalendar", dailyCalendar, true,true);

            await scheduler.ScheduleJob(job, trigger);
        }

 

    public class MyJob : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            await Task.Factory.StartNew(() =>
            {
                var nextTime = context.NextFireTimeUtc?.ToLocalTime();

                var currentTime = DateTime.Now;

                var jobKey = context.JobDetail.Key;

                Console.WriteLine($"JobKey={jobKey},  CurrentTime={currentTime},  FireTime={context.ScheduledFireTimeUtc?.ToLocalTime()},  NextTime={nextTime}");
            });
        }
    }

 

 

 2.WeeklyCalendar

 

可以動態的排除一週內的哪幾天不執行

示例:設定每週的週六和週日不能被執行

        public async static Task Test()
        {
            var scheduler = await StdSchedulerFactory.GetDefaultScheduler();

            await scheduler.Start();

            //DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(13,0,0).DateTime,
            //                                                DateBuilder.DateOf(14,0,0).DateTime);

            WeeklyCalendar calendar = new WeeklyCalendar();

            calendar.SetDayExcluded(DayOfWeek.Sunday,true);
            calendar.SetDayExcluded(DayOfWeek.Saturday, true);

            var job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();

            var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                                               d => d.OnEveryDay()
                                                     .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                                                     .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 00))
                                                     .WithIntervalInSeconds(1)
                                              )
                                               .ModifiedByCalendar("myCalendar")
                                               .Build();

            await scheduler.AddCalendar("myCalendar", calendar, true,true);

            await scheduler.ScheduleJob(job, trigger);
        }

3.HolidayCalendar

動態的排除哪一天不執行

示例:設定今天不能被執行

  public async static Task Test()
        {
            var scheduler = await StdSchedulerFactory.GetDefaultScheduler();

            await scheduler.Start();

            //DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(13,0,0).DateTime,
            //                                                DateBuilder.DateOf(14,0,0).DateTime);

            //WeeklyCalendar calendar = new WeeklyCalendar();

            //calendar.SetDayExcluded(DayOfWeek.Sunday,true);
            //calendar.SetDayExcluded(DayOfWeek.Saturday, true);

            HolidayCalendar calendar = new HolidayCalendar();

            calendar.AddExcludedDate(DateTime.Today);

            var job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();

            var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                                               d => d.OnEveryDay()
                                                     .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                                                     .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 00))
                                                     .WithIntervalInSeconds(1)
                                              )
                                               //.ModifiedByCalendar("myCalendar")
                                               .Build();

            await scheduler.AddCalendar("myCalendar", calendar, true,true);

            await scheduler.ScheduleJob(job, trigger);
        }

4.MonthlyCalendar

排除每個月中的某一天不能被執行

示例:設定每月的第12天不能被執行

   public async static Task Test()
        {
            var scheduler = await StdSchedulerFactory.GetDefaultScheduler();

            await scheduler.Start();

            //DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(13,0,0).DateTime,
            //                                                DateBuilder.DateOf(14,0,0).DateTime);

            //WeeklyCalendar calendar = new WeeklyCalendar();

            //calendar.SetDayExcluded(DayOfWeek.Sunday,true);
            //calendar.SetDayExcluded(DayOfWeek.Saturday, true);

            //HolidayCalendar calendar = new HolidayCalendar();

            //calendar.AddExcludedDate(DateTime.Today);


            //calendar.RemoveExcludedDate(DateTime.Now); 

            MonthlyCalendar calendar = new MonthlyCalendar();

            calendar.SetDayExcluded(12, true);


            var job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();

            var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                                               d => d.OnEveryDay()
                                                     .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                                                     .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 00))
                                                     .WithIntervalInSeconds(1)
                                              )
                                               .ModifiedByCalendar("myCalendar")
                                               .Build();

            await scheduler.AddCalendar("myCalendar", calendar, true,true);

            await scheduler.ScheduleJob(job, trigger);
        }

5.AnnualCalendar

排除每一年中的某一天不被執行

示例:設定每年的7月12日不能被執行

        public async static Task Test()
        {
            var scheduler = await StdSchedulerFactory.GetDefaultScheduler();

            await scheduler.Start();

            //DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(13,0,0).DateTime,
            //                                                DateBuilder.DateOf(14,0,0).DateTime);

            //WeeklyCalendar calendar = new WeeklyCalendar();

            //calendar.SetDayExcluded(DayOfWeek.Sunday,true);
            //calendar.SetDayExcluded(DayOfWeek.Saturday, true);

            //HolidayCalendar calendar = new HolidayCalendar();

            //calendar.AddExcludedDate(DateTime.Today);


            //calendar.RemoveExcludedDate(DateTime.Now); 

            //MonthlyCalendar calendar = new MonthlyCalendar();

            //calendar.SetDayExcluded(12, true);


            AnnualCalendar calendar = new AnnualCalendar();

            calendar.SetDayExcluded(new DateTime(2020,7,12),true);

            var job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();

            var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                                               d => d.OnEveryDay()
                                                     .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                                                     .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 00))
                                                     .WithIntervalInSeconds(1)
                                              )
                                               .ModifiedByCalendar("myCalendar")
                                               .Build();

            await scheduler.AddCalendar("myCalendar", calendar, true,true);

            await scheduler.ScheduleJob(job, trigger);
        }

 

  public virtual void SetDayExcluded(DateTime day, bool exclude)
        {
            DateTime d = new DateTime(FixedYear, day.Month, day.Day, 0, 0, 0);

            if (exclude)
            {
                if (!IsDateTimeExcluded(day, false))
                {
                    excludeDays.Add(d);
                }
            }
            else
            {
                // include
                if (IsDateTimeExcluded(day, false))
                {
                    excludeDays.Remove(d);
                }
            }
        }

6.CronCalendar

以 Cron表示式的方式排除哪些時間不能被執行

示例:設定每月的第12天不能被執行

public async static Task Test()
        {
            var scheduler = await StdSchedulerFactory.GetDefaultScheduler();

            await scheduler.Start();

            //DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(13,0,0).DateTime,
            //                                                DateBuilder.DateOf(14,0,0).DateTime);

            //WeeklyCalendar calendar = new WeeklyCalendar();

            //calendar.SetDayExcluded(DayOfWeek.Sunday,true);
            //calendar.SetDayExcluded(DayOfWeek.Saturday, true);

            //HolidayCalendar calendar = new HolidayCalendar();

            //calendar.AddExcludedDate(DateTime.Today);


            //calendar.RemoveExcludedDate(DateTime.Now); 

            //MonthlyCalendar calendar = new MonthlyCalendar();

            //calendar.SetDayExcluded(12, true);


            //AnnualCalendar calendar = new AnnualCalendar();

            //calendar.SetDayExcluded(new DateTime(2020,7,12),true);

            CronCalendar calendar = new CronCalendar("* * * 12 * ?");



            var job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();

            var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                                               d => d.OnEveryDay()
                                                     .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                                                     .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 00))
                                                     .WithIntervalInSeconds(1)
                                              )
                                               .ModifiedByCalendar("myCalendar")
                                               .Build();

            await scheduler.AddCalendar("myCalendar", calendar, true,true);

            await scheduler.ScheduleJob(job, trigger);
        }

 

相關文章