1

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

  • I would probably add a shutdown hook instead, it's not going to rely on any magic within spring boot, a la this: https://stackoverflow.com/questions/2921945/useful-example-of-a-shutdown-hook-in-java – stringy05 Apr 19 '21 at 22:06
  • actually I have a shutdown hook as well and that didn't work (which is what got me to start looking into the taskScheduler) – andryushka x Apr 19 '21 at 22:12

0 Answers0