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);
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Spring Boot Quartz Starter是一个用于在Spring Boot应用程序中集成Quartz调度框架的库。Quartz是一个功能强大的开源任务调度框架,可以用于在Java应用程序中执行定时任务、计划任务等。 使用Spring Boot Quartz Starter,你可以很方便地将Quartz集成到你的Spring Boot应用程序中。它提供了一些自动配置和便利的功能,让你能够更快地开始使用Quartz。 要使用Spring Boot Quartz Starter,你需要在你的项目添加相应的依赖。在Maven项目中,你可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> ``` 添加了依赖之后,你可以使用Spring Boot提供的注解和配置来定义和管理Quartz任务。通过使用`@EnableScheduling`注解,你可以启用Spring的任务调度功能。然后,你可以使用`@Scheduled`注解来定义定时任务执行规则。 下面是一个简单的示例,演示了如何使用Spring Boot Quartz Starter创建一个定时任务: ```java import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @EnableScheduling public class MyScheduler { @Scheduled(cron = "0 0/5 * * * ?") // 每5分钟执行一次 public void myTask() { // 定时任务的业务逻辑 System.out.println("定时任务执行了!"); } } ``` 在这个示例中,我们创建了一个名为`MyScheduler`的组件,并使用`@EnableScheduling`注解启用了Spring的任务调度功能。然后,我们使用`@Scheduled`注解定义了一个定时任务`myTask()`,它将每5分钟执行一次。 这只是一个简单的示例,你可以根据自己的需求来定义更复杂的定时任务Spring Boot Quartz Starter提供了更多的功能和配置选项,可以帮助你更好地管理和调度任务。 希望能对你有所帮助!如果你有任何其他问题,请随时提问。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值