Skip to content

Commit d5ab03c

Browse files
committed
GH-9524: Expose SourcePollingChannelAdapterSpec.taskScheduler
Fixes: #9524 Issue link: #9524 It is useful in some use-cases to be able to inject a custom `TaskScheduler` (e.g. with a `TaskDecorator`) into a source polling channel adapter. * Add `SourcePollingChannelAdapterFactoryBean.setTaskScheduler()` and call it from the `SourcePollingChannelAdapterSpec.taskScheduler()` * Fix JavaDocs typos in the `ConsumerEndpointSpec` * Test custom `TaskScheduler` usage and mention new option in the `whats-new.adoc`
1 parent be1156a commit d5ab03c

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/SourcePollingChannelAdapterFactoryBean.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.messaging.MessageChannel;
3535
import org.springframework.messaging.core.BeanFactoryMessageChannelDestinationResolver;
3636
import org.springframework.messaging.core.DestinationResolver;
37+
import org.springframework.scheduling.TaskScheduler;
3738
import org.springframework.util.Assert;
3839
import org.springframework.util.StringUtils;
3940

@@ -75,6 +76,8 @@ public class SourcePollingChannelAdapterFactoryBean implements FactoryBean<Sourc
7576

7677
private String role;
7778

79+
private TaskScheduler taskScheduler;
80+
7881
private volatile SourcePollingChannelAdapter adapter;
7982

8083
private volatile boolean initialized;
@@ -111,6 +114,15 @@ public void setRole(String role) {
111114
this.role = role;
112115
}
113116

117+
/**
118+
* Set a {@link TaskScheduler} for polling tasks.
119+
* @param taskScheduler the {@link TaskScheduler} for polling tasks.
120+
* @since 6.4
121+
*/
122+
public void setTaskScheduler(TaskScheduler taskScheduler) {
123+
this.taskScheduler = taskScheduler;
124+
}
125+
114126
/**
115127
* Specify the {@link DestinationResolver} strategy to use.
116128
* The default is a BeanFactoryChannelResolver.
@@ -208,6 +220,9 @@ private void initializeAdapter() {
208220
spca.setBeanName(this.beanName);
209221
spca.setBeanFactory(this.beanFactory);
210222
spca.setTransactionSynchronizationFactory(this.pollerMetadata.getTransactionSynchronizationFactory());
223+
if (this.taskScheduler != null) {
224+
spca.setTaskScheduler(this.taskScheduler);
225+
}
211226
spca.afterPropertiesSet();
212227
this.adapter = spca;
213228
this.initialized = true;

spring-integration-core/src/main/java/org/springframework/integration/dsl/ConsumerEndpointSpec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 the original author or authors.
2+
* Copyright 2016-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -128,7 +128,7 @@ public S role(String role) {
128128

129129
/**
130130
* Configure a {@link TaskScheduler} for scheduling tasks, for example in the
131-
* Polling Consumer. By default the global {@code ThreadPoolTaskScheduler} bean is used.
131+
* Polling Consumer. By default, the global {@code ThreadPoolTaskScheduler} bean is used.
132132
* This configuration is useful when there are requirements to dedicate particular threads
133133
* for polling task, for example.
134134
* @param taskScheduler the {@link TaskScheduler} to use.
@@ -144,7 +144,7 @@ public S taskScheduler(TaskScheduler taskScheduler) {
144144
/**
145145
* Configure a list of {@link MethodInterceptor} objects to be applied, in nested order, to the
146146
* endpoint's handler. The advice objects are applied to the {@code handleMessage()} method
147-
* and therefore to the whole sub-flow afterwards.
147+
* and therefore to the whole sub-flow afterward.
148148
* @param interceptors the advice chain.
149149
* @return the endpoint spec.
150150
* @since 5.3

spring-integration-core/src/main/java/org/springframework/integration/dsl/SourcePollingChannelAdapterSpec.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 the original author or authors.
2+
* Copyright 2016-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import org.springframework.integration.core.MessageSource;
2121
import org.springframework.integration.scheduling.PollerMetadata;
2222
import org.springframework.lang.Nullable;
23+
import org.springframework.scheduling.TaskScheduler;
2324

2425
/**
2526
* @author Artem Bilan
@@ -60,4 +61,15 @@ public SourcePollingChannelAdapterSpec role(String role) {
6061
return this;
6162
}
6263

64+
/**
65+
* Set a {@link TaskScheduler} for polling tasks.
66+
* @param taskScheduler the {@link TaskScheduler} for polling tasks.
67+
* @return the spec
68+
* @since 6.4
69+
*/
70+
public SourcePollingChannelAdapterSpec taskScheduler(TaskScheduler taskScheduler) {
71+
this.endpointFactoryBean.setTaskScheduler(taskScheduler);
72+
return this;
73+
}
74+
6375
}

spring-integration-core/src/test/java/org/springframework/integration/dsl/flows/IntegrationFlowTests.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import org.springframework.integration.store.SimpleMessageStore;
8282
import org.springframework.integration.support.MessageBuilder;
8383
import org.springframework.integration.support.MutableMessageBuilder;
84+
import org.springframework.integration.test.util.TestUtils;
8485
import org.springframework.integration.transformer.PayloadSerializingTransformer;
8586
import org.springframework.integration.util.NoBeansOverrideAnnotationConfigContextLoader;
8687
import org.springframework.messaging.Message;
@@ -94,6 +95,7 @@
9495
import org.springframework.messaging.support.ChannelInterceptor;
9596
import org.springframework.messaging.support.GenericMessage;
9697
import org.springframework.scheduling.TaskScheduler;
98+
import org.springframework.scheduling.concurrent.SimpleAsyncTaskScheduler;
9799
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
98100
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
99101
import org.springframework.stereotype.Component;
@@ -187,10 +189,15 @@ public class IntegrationFlowTests {
187189
@Autowired
188190
AbstractEndpoint stringSupplierEndpoint;
189191

192+
@Autowired
193+
TaskScheduler customScheduler;
194+
190195
@Test
191196
public void testWithSupplierMessageSourceImpliedPoller() {
192197
assertThat(this.stringSupplierEndpoint.isAutoStartup()).isFalse();
193198
assertThat(this.stringSupplierEndpoint.isRunning()).isFalse();
199+
assertThat(TestUtils.getPropertyValue(this.stringSupplierEndpoint, "taskScheduler"))
200+
.isSameAs(this.customScheduler);
194201
this.stringSupplierEndpoint.start();
195202
assertThat(this.suppliedChannel.receive(10000).getPayload()).isEqualTo("FOO");
196203
}
@@ -569,8 +576,14 @@ public Supplier<String> stringSupplier() {
569576
}
570577

571578
@Bean
572-
public IntegrationFlow supplierFlow() {
573-
return IntegrationFlow.fromSupplier(stringSupplier(), c -> c.id("stringSupplierEndpoint"))
579+
public TaskScheduler customScheduler() {
580+
return new SimpleAsyncTaskScheduler();
581+
}
582+
583+
@Bean
584+
public IntegrationFlow supplierFlow(TaskScheduler customScheduler) {
585+
return IntegrationFlow.fromSupplier(stringSupplier(),
586+
c -> c.id("stringSupplierEndpoint").taskScheduler(customScheduler))
574587
.transform(toUpperCaseFunction())
575588
.channel("suppliedChannel")
576589
.get();

src/reference/antora/modules/ROOT/pages/whats-new.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ See xref:spel.adoc[SpEL Support] for more information.
3030
[[x6.4-general]]
3131
=== General Changes
3232

33+
The Java DSL `SourcePollingChannelAdapterSpec` can now be configured with a custom `TaskScheduler`
34+
3335
[[x6.4-remote-files-changes]]
3436
=== Remote File Adapters Changes
3537

0 commit comments

Comments
 (0)