Reputation: 11
I have Schedulers in spring boot application if any one scheduler takes time to execution of process remaining scheduler won't work on given time or after specific time interval.
@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.
}
}
Explaination : method5 & method2 are running every 15 mins. But Suppose if my method 5 tooks more than longer to process then my scheduler(method 2) won't start in next 15 mins. Same way if my method 5 tooks too much time to process and if time of method1 method3 and method4 schedular comes(here eg 1A.M.) but still this schedulers won't run at that time.
Please let me know what to do schedulers to be run perfectly with out any fails.
Upvotes: 1
Views: 3212
Reputation: 5093
By default schedulars in Spring Boot Context is single-threaded. When you need to run parallel tasks, then you use @Configuration class to implement the SchedulingConfigurer interface. This allows access to the underlying ScheduledTaskRegistrar instance. For example, the following example demonstrates how to customize the Executor to execute scheduled tasks parallelly.
@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);
}
}
Upvotes: 2