scheduleBuilder.withMisfireHandlingInstructionDoNothing();
定时任务不触发立即执行,等待下次Cron触发频率到达时刻开始按照Cron频率依次执行
扩展:
scheduleBuilder.withMisfireHandlingInstructionDoNothing(); 不触发立即执行,等待下次Cron触发频率到达时刻开始按照Cron频率依次执行 scheduleBuilder.withMisfireHandlingInstructionFireAndProceed(); 以当前时间为触发频率立刻触发一次执行,然后按照Cron频率依次执行 scheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires(); 以错过的第一个频率时间立刻开始执行,重做错过的所有频率周期后,当下一次触发频率发生时间大于当前时间后,再按照正常的Cron频率依次执行
示例: val trigger = newTrigger(). withSchedule( cronSchedule("0 0 9-17 ? * MON-FRI"). withMisfireHandlingInstructionFireAndProceed() //or other ). build()
In this example the trigger should fire every hour between 9 AM and 5 PM, from Monday to Friday. But once again first two invocations were missed (so the trigger misfired) and this situation was discovered at 10:15 AM. Note that available misfire instructions are different compared to simple triggers:
Instruction | Meaning |
---|---|
smart policy - default | See: withMisfireHandlingInstructionFireAndProceed |
withMisfireHandlingInstructionIgnoreMisfiresMISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY | All misfired executions are immediately executed, then the trigger runs back on schedule. Example scenario: the executions scheduled at 9 and 10 AM are executed immediately. The next scheduled execution (at 11 AM) runs on time. |
withMisfireHandlingInstructionFireAndProceedMISFIRE_INSTRUCTION_FIRE_ONCE_NOW | Immediately executes first misfired execution and discards other (i.e. all misfired executions are merged together). Then back to schedule. No matter how many trigger executions were missed, only single immediate execution is performed. Example scenario: the executions scheduled at 9 and 10 AM are merged and executed only once (in other words: the execution scheduled at 10 AM is discarded). The next scheduled execution (at 11 AM) runs on time. |
withMisfireHandlingInstructionDoNothingMISFIRE_INSTRUCTION_DO_NOTHING | All misfired executions are discarded, the scheduler simply waits for next scheduled time. Example scenario: the executions scheduled at 9 and 10 AM are discarded, so basically nothing happens. The next scheduled execution (at 11 AM) runs on time. |
代码如下:
public void addJob(TaskConfBO bo) throws Exception {
//获去调度器实例
Scheduler scheduler = schedulerFactoryBean.getScheduler();
//新增定时任务
// 构建job信息
JobDetail jobDetail = JobBuilder.newJob(DataAnalysisJob.class)
.withIdentity(bo.getId(), bo.getOrgId()).build();
JobDataMap jobDataMap = jobDetail.getJobDataMap();
jobDataMap.put("id", bo.getId());
// 表达式调度构建器(即任务执行的时间)
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(bo.getCron());
//定时任务不触发立即执行,等待下次Cron触发频率到达时刻开始按照Cron频率依次执行
scheduleBuilder.withMisfireHandlingInstructionDoNothing();
// 按新的cronExpression表达式构建一个新的trigger
TriggerBuilder<CronTrigger> triggerBuilder =TriggerBuilder.newTrigger().withIdentity(bo.getId(), bo.getOrgId())
.withSchedule(scheduleBuilder);
// 默认立即执行
if (bo.getStartTime() != null) {
// 指定时间执行
triggerBuilder.startAt(bo.getStartTime());
}
if (bo.getEndTime() != null) {
// 指定时间结束
triggerBuilder.endAt(bo.getEndTime());
}
CronTrigger trigger = triggerBuilder.build();
//设置job执行
scheduler.scheduleJob(jobDetail, trigger);
}