From a0c80ffc06a096c0863ee9d362dc6086146e3b6f Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 8 Jul 2023 15:55:25 +0200 Subject: [PATCH] Destroy local TaskSchedulerRouter which may contain local executor See gh-20818 --- .../ScheduledAnnotationBeanPostProcessor.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 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 6f9746171c..6bc75b58dd 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 @@ -144,6 +144,9 @@ public class ScheduledAnnotationBeanPostProcessor @Nullable private ApplicationContext applicationContext; + @Nullable + private TaskSchedulerRouter localScheduler; + private final Set> nonAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(64)); private final Map> scheduledTasks = new IdentityHashMap<>(16); @@ -251,10 +254,10 @@ public class ScheduledAnnotationBeanPostProcessor this.registrar.setScheduler(this.scheduler); } else { - TaskSchedulerRouter router = new TaskSchedulerRouter(); - router.setBeanName(this.beanName); - router.setBeanFactory(this.beanFactory); - this.registrar.setTaskScheduler(router); + this.localScheduler = new TaskSchedulerRouter(); + this.localScheduler.setBeanName(this.beanName); + this.localScheduler.setBeanFactory(this.beanFactory); + this.registrar.setTaskScheduler(this.localScheduler); } if (this.beanFactory instanceof ListableBeanFactory lbf) { @@ -637,6 +640,9 @@ public class ScheduledAnnotationBeanPostProcessor } } this.registrar.destroy(); + if (this.localScheduler != null) { + this.localScheduler.destroy(); + } } }