spring-boot-starter-quartz 添加定时任务立即执行一次的问题解决

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:

InstructionMeaning
smart policy - defaultSee: withMisfireHandlingInstructionFireAndProceed
withMisfireHandlingInstructionIgnoreMisfires
MISFIRE_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.
withMisfireHandlingInstructionFireAndProceed
MISFIRE_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.
withMisfireHandlingInstructionDoNothing
MISFIRE_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);
}

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值