diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfiguration.java index 6c02a634277b..f87ace5d99d0 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfiguration.java @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.task; +import java.util.concurrent.ScheduledExecutorService; + import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; @@ -44,7 +46,8 @@ public class TaskSchedulingAutoConfiguration { @Bean @ConditionalOnBean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME) - @ConditionalOnMissingBean({ SchedulingConfigurer.class, TaskScheduler.class }) + @ConditionalOnMissingBean({ SchedulingConfigurer.class, TaskScheduler.class, + ScheduledExecutorService.class }) public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) { return builder.build(); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java index afb863aa2874..456ebb7d101a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java @@ -18,6 +18,8 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import org.junit.Test; @@ -96,6 +98,19 @@ public void enableSchedulingWithExistingTaskSchedulerBacksOff() { }); } + @Test + public void enableSchedulingWithExistingScheduledExecutorServiceBacksOff() { + this.contextRunner.withUserConfiguration(SchedulingConfiguration.class, + ScheduledExecutorServiceConfiguration.class).run((context) -> { + assertThat(context).doesNotHaveBean(TaskScheduler.class); + assertThat(context).hasSingleBean(ScheduledExecutorService.class); + TestBean bean = context.getBean(TestBean.class); + Thread.sleep(15); + assertThat(bean.threadNames) + .allMatch((name) -> name.contains("pool-")); + }); + } + @Test public void enableSchedulingWithConfigurerBacksOff() { this.contextRunner.withUserConfiguration(SchedulingConfiguration.class, @@ -123,6 +138,16 @@ public TaskScheduler customTaskScheduler() { } + @Configuration + static class ScheduledExecutorServiceConfiguration { + + @Bean + public ScheduledExecutorService customScheduledExecutorService() { + return Executors.newScheduledThreadPool(2); + } + + } + @Configuration static class TaskSchedulerCustomizerConfiguration { diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index f663d8eaf81f..67d8aae8688c 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -6382,8 +6382,8 @@ A `ThreadPoolTaskScheduler` can also be auto-configured if need to be associated scheduled task execution (`@EnableScheduling`). The thread pool uses one thread by default and those settings can be fine-tuned using the `spring.task.scheduling` namespace. -Both a `TaskExecutorBuilder` bean and a `TaskSchedulerBuilder` bean are made available in the -context if a custom executor or scheduler needs to be created. +Both a `TaskExecutorBuilder` bean and a `TaskSchedulerBuilder` bean are made available in +the context if a custom executor or scheduler needs to be created.