Skip to content

Commit ad01c44

Browse files
authored
Fix maxMessagesPerPoll for SourcePollingChAdapter (#8747)
* Fix maxMessagesPerPoll for SourcePollingChAdapter The `AbstractMethodAnnotationPostProcessor` does not check for `PollerMetadata.MAX_MESSAGES_UNBOUNDED` before setting `maxMessagesPerPoll` into a `SourcePollingChannelAdapter` which in this case must be `1` Also fix `SourcePollingChannelAdapterFactoryBean` to not mutate the provided `PollerMetadata` (which might be global default) with a new `maxMessagesPerPoll` **Cherry-pick to `6.1.x` & `6.0.x`** * * Fix `this.` prefix in `SourcePollingChannelAdapterFactoryBean`
1 parent 261589a commit ad01c44

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Diff for: spring-integration-core/src/main/java/org/springframework/integration/config/AbstractMethodAnnotationPostProcessor.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,14 @@ protected void configurePollingEndpoint(AbstractPollingEndpoint pollingEndpoint,
687687
pollingEndpoint.setTaskExecutor(pollerMetadata.getTaskExecutor());
688688
pollingEndpoint.setTrigger(pollerMetadata.getTrigger());
689689
pollingEndpoint.setAdviceChain(pollerMetadata.getAdviceChain());
690-
pollingEndpoint.setMaxMessagesPerPoll(pollerMetadata.getMaxMessagesPerPoll());
690+
long maxMessagesPerPoll = pollerMetadata.getMaxMessagesPerPoll();
691+
if (maxMessagesPerPoll == PollerMetadata.MAX_MESSAGES_UNBOUNDED &&
692+
pollingEndpoint instanceof SourcePollingChannelAdapter) {
693+
// the default is 1 since a source might return
694+
// a non-null and non-interruptible value every time it is invoked
695+
maxMessagesPerPoll = 1;
696+
}
697+
pollingEndpoint.setMaxMessagesPerPoll(maxMessagesPerPoll);
691698
pollingEndpoint.setErrorHandler(pollerMetadata.getErrorHandler());
692699
if (pollingEndpoint instanceof PollingConsumer) {
693700
((PollingConsumer) pollingEndpoint).setReceiveTimeout(pollerMetadata.getReceiveTimeout());

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,13 @@ private void initializeAdapter() {
186186
Assert.notNull(this.pollerMetadata, () -> "No poller has been defined for channel-adapter '"
187187
+ this.beanName + "', and no default poller is available within the context.");
188188
}
189-
if (this.pollerMetadata.getMaxMessagesPerPoll() == Integer.MIN_VALUE) {
189+
long maxMessagesPerPoll = this.pollerMetadata.getMaxMessagesPerPoll();
190+
if (maxMessagesPerPoll == PollerMetadata.MAX_MESSAGES_UNBOUNDED) {
190191
// the default is 1 since a source might return
191192
// a non-null and non-interruptible value every time it is invoked
192-
this.pollerMetadata.setMaxMessagesPerPoll(1);
193+
maxMessagesPerPoll = 1;
193194
}
194-
spca.setMaxMessagesPerPoll(this.pollerMetadata.getMaxMessagesPerPoll());
195+
spca.setMaxMessagesPerPoll(maxMessagesPerPoll);
195196
if (this.sendTimeout != null) {
196197
spca.setSendTimeout(this.sendTimeout);
197198
}

Diff for: spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
import org.springframework.integration.endpoint.MethodInvokingMessageSource;
106106
import org.springframework.integration.endpoint.PollingConsumer;
107107
import org.springframework.integration.endpoint.ReactiveStreamsConsumer;
108+
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
108109
import org.springframework.integration.expression.SpelPropertyAccessorRegistrar;
109110
import org.springframework.integration.gateway.GatewayProxyFactoryBean;
110111
import org.springframework.integration.handler.ServiceActivatingHandler;
@@ -417,12 +418,15 @@ public void testAnnotatedServiceActivator() throws Exception {
417418

418419
assertThat(this.counterChannel.receive(10)).isNull();
419420

420-
SmartLifecycle countSA = this.context.getBean("annotationTestService.count.inboundChannelAdapter",
421-
SmartLifecycle.class);
421+
SourcePollingChannelAdapter countSA =
422+
this.context.getBean("annotationTestService.count.inboundChannelAdapter",
423+
SourcePollingChannelAdapter.class);
422424
assertThat(countSA.isAutoStartup()).isFalse();
423425
assertThat(countSA.getPhase()).isEqualTo(23);
424426
countSA.start();
425427

428+
assertThat(countSA.getMaxMessagesPerPoll()).isEqualTo(1);
429+
426430
for (int i = 0; i < 10; i++) {
427431
Message<?> message = this.counterChannel.receive(10_000);
428432
assertThat(message).isNotNull();

0 commit comments

Comments
 (0)