Spring - Scheduled Task - Graceful Shutdown

user1491636 :

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

Is there a solution for allowing a task which is currently being executed to complete - or at least given some time to complete - before the life cycle reaches the pre-destroy phase?

Matej Marconak :

You need update configuration of ThreadPoolTaskScheduler. Set true for waitForJobsToCompleteOnShutdown (method setWaitForTasksToCompleteOnShutdown).

From documentation:

Set whether to wait for scheduled tasks to complete on shutdown, not interrupting running tasks and executing all tasks in the queue. Default is "false", shutting down immediately through interrupting ongoing tasks and clearing the queue. Switch this flag to "true" if you prefer fully completed tasks at the expense of a longer shutdown phase.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=476236&siteId=1
Recommended