From 0ea8e1631f7489b8f92bc3513ad2bb9ca09c5dc5 Mon Sep 17 00:00:00 2001 From: Guram Savinov Date: Wed, 14 Sep 2022 18:06:44 +0300 Subject: [PATCH] Shutdown tasks annotated with @Scheduled when waitForTasksToCompleteOnShutdown is true 1. ScheduledTaskRegistrar: shutdown taskScheduler of ExecutorConfigurationSupport type in destroy() 2. ScheduledAnnotationBeanPostProcessor: do not cancel tasks in destroy(), taskRegistrar cancel them Closes gh-26482 --- .../annotation/ScheduledAnnotationBeanPostProcessor.java | 8 +------- .../scheduling/config/ScheduledTaskRegistrar.java | 4 ++++ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java index facf5323d035..61a3d22fcbd4 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java @@ -593,16 +593,10 @@ public boolean requiresDestruction(Object bean) { @Override public void destroy() { + this.registrar.destroy(); synchronized (this.scheduledTasks) { - Collection> allTasks = this.scheduledTasks.values(); - for (Set tasks : allTasks) { - for (ScheduledTask task : tasks) { - task.cancel(); - } - } this.scheduledTasks.clear(); } - this.registrar.destroy(); } } diff --git a/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java b/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java index dd692b1374e9..fff031ce2c0f 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java +++ b/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java @@ -34,6 +34,7 @@ import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler; +import org.springframework.scheduling.concurrent.ExecutorConfigurationSupport; import org.springframework.scheduling.support.CronTrigger; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -552,6 +553,9 @@ public Set getScheduledTasks() { @Override public void destroy() { + if (this.taskScheduler instanceof ExecutorConfigurationSupport) { + ((ExecutorConfigurationSupport) this.taskScheduler).shutdown(); + } for (ScheduledTask task : this.scheduledTasks) { task.cancel(); }