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); }