第六节: 六类Calander处理六种不同的时间场景

时间:2021-01-16 20:55:42

背景介绍及其使用

  该章节主要补充介绍,在前一章四类触发器的基础上配合六大Canlander来动态删减某些时间,来满足更多的应用场景。

1. DailyCalendar:动态排除某天的某些字段.

(需求:每天8-23点执行,每隔1s执行一次,但是21-22点这个区间不执行)

2. WeeklyCalendar:适合在星期几的维度上做“减法操作”

(需求:每天8-23点执行,每隔1s执行一次,但是周五这一天不执行)

3. HolidayCalendar:适合当年的某一天不能执行

(需求:每天8-23点执行,每隔1s执行一次,但是今年的6月16号这一天不执行)

4. MonthlyCalendar:适合某个月中的某一天不能执行

(需求:每天8-23点执行,每隔1s执行一次,但是每月的27号不执行)

5. AnnualCalendar:适合每年指定的某一天不能执行(有问题)

(需求:每天8-23点执行,每隔1s执行一次,但是每年的6月16号这一天不执行)

6. CronCalendar:字符串表达式来排除某一天,某一个月份,某一年都可以

(需求:每天8-23点执行,每隔1s执行一次,但是2月27号这天不执行)

 代码分享:

        public static void CalanderShow()
{
//1. 每天8-23点执行,每隔1s执行一次,但是21-22点这个区间不执行
{
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
//单独记录一个区间段 21-22点
DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(, , ).DateTime,
DateBuilder.DateOf(, , ).DateTime);
scheduler.AddCalendar("mycalendar", dailyCalendar, true, true); var job = JobBuilder.Create<HelloJob>().Build();
var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
x => x.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(, ))
.EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(, ))
.WithIntervalInSeconds()
)
.ModifiedByCalendar("mycalendar")
.Build();
scheduler.ScheduleJob(job, trigger);
} //2. 每天8-23点执行,每隔1s执行一次,但是周五这一天不执行
{
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
//设定周五不能执行
WeeklyCalendar calendar = new WeeklyCalendar();
calendar.SetDayExcluded(DayOfWeek.Friday, true);
scheduler.AddCalendar("mycalendar", calendar, true, true); var job = JobBuilder.Create<HelloJob>().Build();
var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
x => x.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(, ))
.EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(, ))
.WithIntervalInSeconds()
)
.ModifiedByCalendar("mycalendar")
.Build();
scheduler.ScheduleJob(job, trigger);
} //3. 每天8-23点执行,每隔1s执行一次,但是当年6月16号这一天不执行
{
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
HolidayCalendar calendar = new HolidayCalendar();
calendar.AddExcludedDate(DateTime.Parse("06-16")); //把当年6月16日排除在外 scheduler.AddCalendar("mycalendar", calendar, true, true);
var job = JobBuilder.Create<HelloJob>().Build();
var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
x => x.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(, ))
.EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(, ))
.WithIntervalInSeconds()
)
.ModifiedByCalendar("mycalendar")
.Build();
scheduler.ScheduleJob(job, trigger); } //4. 每天8-23点执行,每隔1s执行一次,但是每月的27号不执行
{
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start(); //指定月份中的某一天不能执行
MonthlyCalendar calendar = new MonthlyCalendar();
calendar.SetDayExcluded(, true); //将27号这天排除在外
scheduler.AddCalendar("mycalendar", calendar, true, true); var job = JobBuilder.Create<HelloJob>().Build();
var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
x => x.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(, ))
.EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(, ))
.WithIntervalInSeconds()
)
.ModifiedByCalendar("mycalendar")
.Build(); scheduler.ScheduleJob(job, trigger);
} //5. 每天8-23点执行,每隔1s执行一次,但是每年的6月16号这一天不执行
{
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start(); AnnualCalendar calendar = new AnnualCalendar();
calendar.SetDayExcluded(DateTime.Parse("06-16"), true); //把每年的6月16日排除在外
scheduler.AddCalendar("mycalendar", calendar, true, true); var job = JobBuilder.Create<HelloJob>().Build();
var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
x => x.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(, ))
.EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(, ))
.WithIntervalInSeconds()
)
.ModifiedByCalendar("mycalendar")
.Build();
scheduler.ScheduleJob(job, trigger);
} //6.每天8-23点执行,每隔1s执行一次,但是2月27号这天不执行
{
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start(); CronCalendar calendar = new CronCalendar("* * * 27 2 ?");
scheduler.AddCalendar("mycalendar", calendar, true, true); var job = JobBuilder.Create<HelloJob>().Build();
var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
x => x.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(, ))
.EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(, ))
.WithIntervalInSeconds()
)
.ModifiedByCalendar("mycalendar")
.Build();
scheduler.ScheduleJob(job, trigger);
} }

第六节: 六类Calander处理六种不同的时间场景