Spring graceful shutdown schedule does not work when using scheduled cron

I have a small standalone application that sets the scheduler to gracefully finish. With the following configuration:

@Bean
public TaskScheduler taskScheduler() {
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setWaitForTasksToCompleteOnShutdown(true);
    scheduler.setAwaitTerminationSeconds(60);
    return scheduler;
}

      

I can get it to gracefully complete the scheduler, but only if I don't have the @Scheduled (cron =) task. Once I have one of these, no matter what the scheduler gets stuck before timeout. I've already tried setting it up also with an executor and doing shutdown / wait manually and the effect will be exactly the same.

These cron jobs don't even work. They are set to run at specific times during the night, for example.

Spring version: 4.2.8.RELEASE

This will happen when the timeout reaches the end:

2017.07.28 01:44:56 [Thread-3] WARN  Timed out while waiting for executor 'taskScheduler' to terminate

      

Any thoughts?

+3


source to share


2 answers


Possible bug in this version of Spring? See jira.spring.io/browse/SPR-15067



+2


source


Because, by default, ScheduledThreadPoolExecutor will wait for all pending scheduled tasks to complete, even if the scheduled tasks are not running at that time.

Try this below:



@Bean
public TaskScheduler taskScheduler() {
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler() {
        private static final long serialVersionUID = -1L;
        @Override
        public void destroy() {
            this.getScheduledThreadPoolExecutor().setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
            super.destroy();
        }
    };
    scheduler.setWaitForTasksToCompleteOnShutdown(true);
    scheduler.setAwaitTerminationSeconds(60);
    return scheduler;
}

      

Then the ScheduledThreadPoolExecutor will only wait for the scheduled tasks that are currently running to complete execution.

0


source







All Articles