【问题标题】:Quartz scheduler with excluding specific time range不包括特定时间范围的 Quartz 调度程序
【发布时间】:2016-05-05 18:56:34
【问题描述】:

我想通过 Quartz 调度器进行调度,不包括特定的时间范围。

例如:凌晨 2 点 - 凌晨 3 点和下午 5:30 - 下午 5:45

在不使用 cron 表达式和 cron 调度程序的情况下,还有其他方法可以实现吗?

【问题讨论】:

  • 您为什么不想使用 cron 调度程序?另外,我不是 100% 清楚你想归档什么,你能改写你的例子吗?
  • @dquijada 我不擅长 cron 表达式,想知道,quartz-scheduler api 中还有其他库可以完成这项工作吗?最后,将解析 cron 表达式,并将解析的值设置为对象的变量。所以,假设应该有一些 setter 方法来做同样的事情!

标签: java quartz-scheduler


【解决方案1】:

这就是石英日历的用途。由于您希望排除特定的 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");

...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多