I created a SpringBoot 2.4.2 application that has several long running tasks that should not be terminated while they are still running.
I have defined the Scheduler like this:
@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(10);
scheduler.setThreadNamePrefix("my-scheduled-task-pool ");
scheduler.setAwaitTerminationSeconds(30);
scheduler.setWaitForTasksToCompleteOnShutdown(true);
scheduler.initialize();
taskRegistrar.setTaskScheduler(scheduler);
}
}
Then I have an @Service class in a seperate package that contains a method like this:
@Scheduled(fixedDelay = 5000)
public void myTask() {
someLongRunningTask();
System.out.println("Finished Task")
}
When I terminate the Program by either doing CTRL+C in the Terminal or calling kill -15 {$pid}, spring shuts down before the myTask() method is finished.
Is there a way I can force Spring to always wait for all of my Tasks to finish before Shutting down? I was under the assumption that scheduler.setWaitForTasksToCompleteOnShutdown(true) took care of this.
Thanks a lot for your help