Spring - Scheduled task - Graceful completion

I have a Spring-Boot application with a bean that runs a scheduled task at 1 minute intervals, and this bean has a method @PreDestroy.

Is there a solution to the task that is currently being completed, to complete, or at least give some time to complete, before the life cycle reaches the phase before destruction?

+4
source share
3 answers

You need update settings ThreadPoolTaskScheduler. Set true to waitForJobsToCompleteOnShutdown(method setWaitForTasksToCompleteOnShutdown).

From the documentation:

, , . "false", , . "true", .

+2

@Matej . , ,

 @Bean
  public ThreadPoolTaskScheduler setSchedulerToWait(ThreadPoolTaskScheduler threadPoolTaskScheduler){
   threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
   return threadPoolTaskScheduler;
 }
+2

Spring Boot 2.1.0, :

@Bean
TaskSchedulerCustomizer taskSchedulerCustomizer() {
    return taskScheduler -> {
        taskScheduler.setAwaitTerminationSeconds(60);
        taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
    };
}

TaskSchedulerCustomizer ThreadPoolTaskScheduler

:

  1. ExecutorConfigurationSupport
  2. TaskSchedulerCustomizer
+1

Source: https://habr.com/ru/post/1675832/


All Articles