如果任何一个调度程序都花时间来执行剩余的调度程序,则我在Spring Boot应用程序中有调度程序,那么在给定的时间或特定的时间间隔后,剩余的调度程序将无法正常工作。
@Component
public class ScheduledTask(){
@Scheduled(cron ="0 00 01 1/1 * ? ")
public void method1(){
//process 1 do something.
}
@Scheduled(initialDelay =5000, fixedRate=900000)
public void method2(){
//process 2 do something.
}
@Scheduled(cron ="0 10 00 1/1 * ? ")
public void method3(){
//process 3 do something.
}
@Scheduled(cron ="0 10 00 1/1 * ? ")
public void method4(){
//process 4 do something.
}
@Scheduled(initialDelay =5000, fixedRate=900000)
public void method5(){
//process 5 do something.
}
}
说明:method5和method2每15分钟运行一次。但是,如果我的方法5花费了更多的时间来处理,那么我的调度程序(方法2)将在接下来的15分钟内启动。如果我的方法5花费太多时间来处理,并且方法1,方法3和方法4的计划时间到了(例如1A.M.),则该方法相同,但该计划程序那时将不会运行。
请让我知道如何做才能使调度程序完美运行而不会失败。
默认情况下,Spring Boot上下文中的时间表是单线程的。当需要运行并行任务时,可以使用@Configuration类来实现SchedulingConfigurer接口。这允许访问基础ScheduledTaskRegistrar实例。例如,以下示例演示了如何自定义执行器以执行并行安排任务。
@Configuration
@EnableScheduling
public class AppConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
@Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(100);
}
}