1
votes

I'm using Spring Boot + Spring Task Scheduler to creating a polling application. I plan to use the actuator to shutdown the application during maintenance windows (see shutdown endpoint for actuator: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-endpoints).

My question is, will the Spring be smart enough finish an in-flight task before gracefully shutting down the application? Will Spring be smart enough not to start another task if the in-flight task completes before shutdown?

Thank you.

2
I know that shutdown waits 60 (millis?) by default before shutting down the application.Eric

2 Answers

1
votes

You can control this by setting

ThreadPoolTaskExecutor#waitForTasksToCompleteOnShutdown

to true.

There is a really good explanation in the javadoc of the inherited setter

org.springframework.scheduling.concurrent.ExecutorConfigurationSupport#setWaitForTasksToCompleteOnShutdown

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...

The spring scheduler uses per default a

java.util.concurrent.ThreadPoolExecutor

which will not start new tasks during shutdown as described in javadoc of

java.util.concurrent.ThreadPoolExecutor#shutdown

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted...

3
votes

Since spring boot 2.1 you can set the following property

# Whether to wait for running jobs to complete on shutdown.
spring.quartz.wait-for-jobs-to-complete-on-shutdown=false