I have a spring boot application which runs tasks on a schedule using the @Scheduled
annotation.
On shutdown if the scheduled task is running, I want to allow it finish.
I first added a shutdown hook to the java runtime (Runtime.getRuntime().addShutdownHook( ... )
) to simply wait -- but my tasks were still getting interrupted -- pretty much immediately.
So then I started looking into if this had to do with the scheduled task and tried this:
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setWaitForTasksToCompleteOnShutdown(true);
scheduler.setAwaitTerminationSeconds(60);
scheduler.initialize();
return scheduler;
}
The problem is that this doesn't actually seem to be protecting the code that is running from thread interruption either and I am still getting Unexpected error occurred in scheduled task
errors where the cause is java.lang.InterruptedException
and com.amazonaws.http.timers.client.SdkInterruptedException