Skip to content

Commit dca6435

Browse files
committed
Polish "Make Scheduler consistent for Spring Integration"
See gh-25109
1 parent c5491cf commit dca6435

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.integration.config.EnableIntegration;
4747
import org.springframework.integration.config.EnableIntegrationManagement;
4848
import org.springframework.integration.config.IntegrationManagementConfigurer;
49+
import org.springframework.integration.context.IntegrationContextUtils;
4950
import org.springframework.integration.gateway.GatewayProxyFactoryBean;
5051
import org.springframework.integration.jdbc.store.JdbcMessageStore;
5152
import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport;
@@ -58,7 +59,6 @@
5859
import org.springframework.messaging.rsocket.RSocketRequester;
5960
import org.springframework.messaging.rsocket.RSocketStrategies;
6061
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
61-
import org.springframework.scheduling.TaskScheduler;
6262
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
6363
import org.springframework.util.StringUtils;
6464

@@ -81,26 +81,27 @@
8181
public class IntegrationAutoConfiguration {
8282

8383
/**
84-
* The {@link TaskScheduler} configuration.
84+
* Basic Spring Integration configuration.
8585
*/
8686
@Configuration(proxyBeanMethods = false)
87-
@ConditionalOnBean(TaskSchedulerBuilder.class)
88-
protected static class IntegrationTaskSchedulerConfiguration {
89-
90-
@Bean
91-
@ConditionalOnMissingBean
92-
public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) {
93-
return builder.build();
94-
}
87+
@EnableIntegration
88+
protected static class IntegrationConfiguration {
9589

9690
}
9791

9892
/**
99-
* Basic Spring Integration configuration.
93+
* Expose a standard {@link ThreadPoolTaskScheduler} if the user has not enabled task
94+
* scheduling explicitly.
10095
*/
10196
@Configuration(proxyBeanMethods = false)
102-
@EnableIntegration
103-
protected static class IntegrationConfiguration {
97+
@ConditionalOnBean(TaskSchedulerBuilder.class)
98+
@ConditionalOnMissingBean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)
99+
protected static class IntegrationTaskSchedulerConfiguration {
100+
101+
@Bean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)
102+
public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) {
103+
return builder.build();
104+
}
104105

105106
}
106107

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfigurationTests.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@
7474
class IntegrationAutoConfigurationTests {
7575

7676
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
77-
.withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class, IntegrationAutoConfiguration.class,
78-
TaskSchedulingAutoConfiguration.class));
77+
.withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class, IntegrationAutoConfiguration.class));
7978

8079
@Test
8180
void integrationIsAvailable() {
@@ -226,14 +225,29 @@ void rsocketSupportEnabled() {
226225
}
227226

228227
@Test
229-
void taskSchedulerAutoConfigured() {
230-
this.contextRunner
228+
void taskSchedulerIsNotOverridden() {
229+
this.contextRunner.withConfiguration(AutoConfigurations.of(TaskSchedulingAutoConfiguration.class))
231230
.withPropertyValues("spring.task.scheduling.thread-name-prefix=integration-scheduling-",
232231
"spring.task.scheduling.pool.size=3")
233-
.run((context) -> assertThat(context)
234-
.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)
235-
.hasFieldOrPropertyWithValue("threadNamePrefix", "integration-scheduling-")
236-
.hasFieldOrPropertyWithValue("scheduledExecutor.corePoolSize", 3));
232+
.run((context) -> {
233+
assertThat(context).hasSingleBean(TaskScheduler.class);
234+
assertThat(context).getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)
235+
.hasFieldOrPropertyWithValue("threadNamePrefix", "integration-scheduling-")
236+
.hasFieldOrPropertyWithValue("scheduledExecutor.corePoolSize", 3);
237+
});
238+
}
239+
240+
@Test
241+
void taskSchedulerCanBeCustomized() {
242+
TaskScheduler customTaskScheduler = mock(TaskScheduler.class);
243+
this.contextRunner.withConfiguration(AutoConfigurations.of(TaskSchedulingAutoConfiguration.class))
244+
.withBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class,
245+
() -> customTaskScheduler)
246+
.run((context) -> {
247+
assertThat(context).hasSingleBean(TaskScheduler.class);
248+
assertThat(context).getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)
249+
.isSameAs(customTaskScheduler);
250+
});
237251
}
238252

239253
@Configuration(proxyBeanMethods = false)

spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6249,7 +6249,7 @@ The auto-configured `TaskExecutorBuilder` allows you to easily create instances
62496249
====
62506250

62516251
The thread pool uses 8 core threads that can grow and shrink according to the load.
6252-
Those default settings can be fine-tuned using the `spring.task.execution` namespace as shown in the following example:
6252+
Those default settings can be fine-tuned using the `spring.task.execution` namespace, as shown in the following example:
62536253

62546254
[source,yaml,indent=0,configprops,configblocks]
62556255
----
@@ -6265,8 +6265,18 @@ Those default settings can be fine-tuned using the `spring.task.execution` names
62656265
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.
62666266
Shrinking of the pool is more aggressive as threads are reclaimed when they are idle for 10 seconds (rather than 60 seconds by default).
62676267

6268-
A `ThreadPoolTaskScheduler` can also be auto-configured if need to be associated to scheduled task execution (`@EnableScheduling`).
6269-
The thread pool uses one thread by default and those settings can be fine-tuned using the `spring.task.scheduling` namespace.
6268+
A `ThreadPoolTaskScheduler` can also be auto-configured if need to be associated to scheduled task execution (e.g. `@EnableScheduling`).
6269+
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:
6270+
6271+
[source,yaml,indent=0,configprops,configblocks]
6272+
----
6273+
spring:
6274+
task:
6275+
scheduling:
6276+
thread-name-prefix: "scheduling-"
6277+
pool:
6278+
size: 2
6279+
----
62706280

62716281
Both a `TaskExecutorBuilder` bean and a `TaskSchedulerBuilder` bean are made available in the context if a custom executor or scheduler needs to be created.
62726282

@@ -6278,9 +6288,7 @@ Spring Boot offers several conveniences for working with {spring-integration}[Sp
62786288
Spring Integration provides abstractions over messaging and also other transports such as HTTP, TCP, and others.
62796289
If Spring Integration is available on your classpath, it is initialized through the `@EnableIntegration` annotation.
62806290

6281-
Spring Integration polling logic is based on the `TaskScheduler`.
6282-
So, it relies on the auto-configured one (see the previous section), or exposes a `taskScheduler` bean according provided `spring.task.scheduling` configuration properties.
6283-
If only Spring Integration is used, the `@EnableScheduling` annotation is optional and can be omitted on the target configuration classes.
6291+
Spring Integration polling logic relies <<boot-features-task-execution-scheduling,on the auto-configured `TaskScheduler`>>.
62846292

62856293
Spring Boot also configures some features that are triggered by the presence of additional Spring Integration modules.
62866294
If `spring-integration-jmx` is also on the classpath, message processing statistics are published over JMX.

0 commit comments

Comments
 (0)