I want to implement multiple @Scheduled(with fixed delay) tasks, each with their own thread pools.
@Scheduled(fixedDelayString = "30000")
public void createOrderSchedule() {
//create 10 orders concurrently; wait for all to be finished
createOrder(10);
}
@Scheduled(fixedDelayString = "30000")
public void processOrderSchedule() {
//process 10 orders concurrently; wait for all to be finished
}
@Scheduled(fixedDelayString = "30000")
public void notifySchedule() {
//send notification for 10 orders concurrently; wait for all to be finished
}
I managed to create different ThreadPoolTaskExecutor
for every scheduler as below:
@Bean("orderPool")
public ThreadPoolTaskExecutor createOrderTaskExecutor() {
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setCorePoolSize(5);
pool.setMaxPoolSize(10);
pool.setThreadNamePrefix("order-thread-pool-");
pool.setWaitForTasksToCompleteOnShutdown(true);
return pool;
}
..
I provided @Async
over each task.
@Async("orderPool")
public void createOrder(Integer noOforders) {..}
and a task scheduler config
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(3);
return threadPoolTaskScheduler;
}
I used CompletableFuture.allOf(..).join();
to wait for each task to complete, but it blocks every other @Scheduled
tasks.
To sum up, I want to achieve following:
- Each
@Scheduled
tasks should run independently without blocking other@Scheduled
tasks. - Each
@Scheduled
tasks should have it's own thread pool, so that it can run multiple sub tasks(say 10) concurrently. - Each
@Scheduled
tasks must wait for each trigger to complete without getting invoked again.
How can I achieve this?