diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java index 5fa58d50e33..c0ef5e18cb7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java @@ -46,6 +46,7 @@ import org.springframework.core.io.ResourceLoader; import org.springframework.integration.config.EnableIntegration; import org.springframework.integration.config.EnableIntegrationManagement; import org.springframework.integration.config.IntegrationManagementConfigurer; +import org.springframework.integration.context.IntegrationContextUtils; import org.springframework.integration.gateway.GatewayProxyFactoryBean; import org.springframework.integration.jdbc.store.JdbcMessageStore; import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport; @@ -58,7 +59,6 @@ import org.springframework.integration.rsocket.outbound.RSocketOutboundGateway; import org.springframework.messaging.rsocket.RSocketRequester; import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; -import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.util.StringUtils; @@ -80,21 +80,6 @@ import org.springframework.util.StringUtils; TaskSchedulingAutoConfiguration.class }) public class IntegrationAutoConfiguration { - /** - * The {@link TaskScheduler} configuration. - */ - @Configuration(proxyBeanMethods = false) - @ConditionalOnBean(TaskSchedulerBuilder.class) - protected static class IntegrationTaskSchedulerConfiguration { - - @Bean - @ConditionalOnMissingBean - public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) { - return builder.build(); - } - - } - /** * Basic Spring Integration configuration. */ @@ -104,6 +89,22 @@ public class IntegrationAutoConfiguration { } + /** + * Expose a standard {@link ThreadPoolTaskScheduler} if the user has not enabled task + * scheduling explicitly. + */ + @Configuration(proxyBeanMethods = false) + @ConditionalOnBean(TaskSchedulerBuilder.class) + @ConditionalOnMissingBean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME) + protected static class IntegrationTaskSchedulerConfiguration { + + @Bean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME) + public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) { + return builder.build(); + } + + } + /** * Spring Integration JMX configuration. */ diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfigurationTests.java index 0c30e71b3c8..a6b9c0eaa74 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfigurationTests.java @@ -74,8 +74,7 @@ import static org.mockito.Mockito.mock; class IntegrationAutoConfigurationTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class, IntegrationAutoConfiguration.class, - TaskSchedulingAutoConfiguration.class)); + .withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class, IntegrationAutoConfiguration.class)); @Test void integrationIsAvailable() { @@ -226,14 +225,29 @@ class IntegrationAutoConfigurationTests { } @Test - void taskSchedulerAutoConfigured() { - this.contextRunner + void taskSchedulerIsNotOverridden() { + this.contextRunner.withConfiguration(AutoConfigurations.of(TaskSchedulingAutoConfiguration.class)) .withPropertyValues("spring.task.scheduling.thread-name-prefix=integration-scheduling-", "spring.task.scheduling.pool.size=3") - .run((context) -> assertThat(context) - .getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class) - .hasFieldOrPropertyWithValue("threadNamePrefix", "integration-scheduling-") - .hasFieldOrPropertyWithValue("scheduledExecutor.corePoolSize", 3)); + .run((context) -> { + assertThat(context).hasSingleBean(TaskScheduler.class); + assertThat(context).getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class) + .hasFieldOrPropertyWithValue("threadNamePrefix", "integration-scheduling-") + .hasFieldOrPropertyWithValue("scheduledExecutor.corePoolSize", 3); + }); + } + + @Test + void taskSchedulerCanBeCustomized() { + TaskScheduler customTaskScheduler = mock(TaskScheduler.class); + this.contextRunner.withConfiguration(AutoConfigurations.of(TaskSchedulingAutoConfiguration.class)) + .withBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class, + () -> customTaskScheduler) + .run((context) -> { + assertThat(context).hasSingleBean(TaskScheduler.class); + assertThat(context).getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME) + .isSameAs(customTaskScheduler); + }); } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index 5c8ea766c61..7398be08b78 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -6249,7 +6249,7 @@ The auto-configured `TaskExecutorBuilder` allows you to easily create instances ==== The thread pool uses 8 core threads that can grow and shrink according to the load. -Those default settings can be fine-tuned using the `spring.task.execution` namespace as shown in the following example: +Those default settings can be fine-tuned using the `spring.task.execution` namespace, as shown in the following example: [source,yaml,indent=0,configprops,configblocks] ---- @@ -6265,8 +6265,18 @@ Those default settings can be fine-tuned using the `spring.task.execution` names This changes the thread pool to use a bounded queue so that when the queue is full (100 tasks), the thread pool increases to maximum 16 threads. Shrinking of the pool is more aggressive as threads are reclaimed when they are idle for 10 seconds (rather than 60 seconds by default). -A `ThreadPoolTaskScheduler` can also be auto-configured if need to be associated to 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. +A `ThreadPoolTaskScheduler` can also be auto-configured if need to be associated to scheduled task execution (e.g. `@EnableScheduling`). +The thread pool uses one thread by default and its settings can be fine-tuned using the `spring.task.scheduling` namespace, as shown in the following example: + +[source,yaml,indent=0,configprops,configblocks] +---- + spring: + task: + scheduling: + thread-name-prefix: "scheduling-" + pool: + size: 2 +---- Both a `TaskExecutorBuilder` bean and a `TaskSchedulerBuilder` bean are made available in the context if a custom executor or scheduler needs to be created. @@ -6278,9 +6288,7 @@ Spring Boot offers several conveniences for working with {spring-integration}[Sp Spring Integration provides abstractions over messaging and also other transports such as HTTP, TCP, and others. If Spring Integration is available on your classpath, it is initialized through the `@EnableIntegration` annotation. -Spring Integration polling logic is based on the `TaskScheduler`. -So, it relies on the auto-configured one (see the previous section), or exposes a `taskScheduler` bean according provided `spring.task.scheduling` configuration properties. -If only Spring Integration is used, the `@EnableScheduling` annotation is optional and can be omitted on the target configuration classes. +Spring Integration polling logic relies <>. Spring Boot also configures some features that are triggered by the presence of additional Spring Integration modules. If `spring-integration-jmx` is also on the classpath, message processing statistics are published over JMX.