排除特定时间范围的石英调度程序
Quartz scheduler with excluding specific time range
我想通过排除特定时间范围的 Quartz 调度程序进行调度。
例如:凌晨 2 点 - 凌晨 3 点和 5:30pm - 5:45pm
有没有其他方法可以不使用 cron 表达式和 cron 调度程序来实现此目的?
这就是石英日历的用途。由于在您的情况下您想排除特定的 daytime 范围,因此您需要使用 DailyCalendar.
单个 DailyCalendar 可以排除单个日期时间范围。由于您想排除多个(两个)范围,因此您需要像这样组合两个 DailyCalendars:
// calendar that excludes the 2am-3am day time range
DailyCalendar dc1 = new DailyCalendar(2,0,0,0,3,0,0,0);
// calendar that excludes the 5:30pm-5:45pm day time range
DailyCalendar dc2 = new DailyCalendar(17,30,0,0,17,45,0,0);
// combine the two calendars so that both ranges are excluded by dc2
dc2.setBaseCalendar(dc1);
// register the calendar with the scheduler
scheduler.addCalendar("MyExcludedDayTimeRangesCalendar", dc2, true, true);
MutableTrigger trigger = ... create SimpleTrigger / CronTrigger / DailyTimeInterval / CalendarIntervalTrigger instance
// associate the created trigger with the registered calendar - the trigger will exclude calendar's time ranges
trigger.setCalendarName("MyExcludedDayTimeRangesCalendar");
...
我想通过排除特定时间范围的 Quartz 调度程序进行调度。
例如:凌晨 2 点 - 凌晨 3 点和 5:30pm - 5:45pm
有没有其他方法可以不使用 cron 表达式和 cron 调度程序来实现此目的?
这就是石英日历的用途。由于在您的情况下您想排除特定的 daytime 范围,因此您需要使用 DailyCalendar.
单个 DailyCalendar 可以排除单个日期时间范围。由于您想排除多个(两个)范围,因此您需要像这样组合两个 DailyCalendars:
// calendar that excludes the 2am-3am day time range
DailyCalendar dc1 = new DailyCalendar(2,0,0,0,3,0,0,0);
// calendar that excludes the 5:30pm-5:45pm day time range
DailyCalendar dc2 = new DailyCalendar(17,30,0,0,17,45,0,0);
// combine the two calendars so that both ranges are excluded by dc2
dc2.setBaseCalendar(dc1);
// register the calendar with the scheduler
scheduler.addCalendar("MyExcludedDayTimeRangesCalendar", dc2, true, true);
MutableTrigger trigger = ... create SimpleTrigger / CronTrigger / DailyTimeInterval / CalendarIntervalTrigger instance
// associate the created trigger with the registered calendar - the trigger will exclude calendar's time ranges
trigger.setCalendarName("MyExcludedDayTimeRangesCalendar");
...