Quartz调度程序,不包括特定的时间范围

时间:2016-04-25 13:33:10

标签: java quartz-scheduler

我想通过排除特定时间范围的Quartz调度程序进行安排。

前:凌晨2点 - 凌晨3点,下午5点30分 - 下午5点45分

如果没有使用cron表达式和cron调度程序,还有其他方法可以实现吗?

1 个答案:

答案 0 :(得分:1)

这就是Quartz Calendars的用途。由于您要排除特定的日间范围,因此您需要使用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");

...