Skip to content

Commit 11e0ea1

Browse files
committed
GH-8856: Adjust phase for TaskScheduler global bean
Fixes: #8856 The `ThreadPoolTaskScheduler` in Spring Framework manages now a lifecycle with a `Integer.MAX_VALUE` phase by default. This causes a wait block for scheduled tasks in the `AbstractPollingEndpoint` instances. These tasks are cancelled by in the later `Integer.MAX_VALUE / 2` phase. So, we have a lifecycle deadlock on context close * Fix `DefaultConfiguringBeanFactoryPostProcessor.registerTaskScheduler()` with a `.addPropertyValue("phase", SmartLifecycle.DEFAULT_PHASE / 2)` to let the `AbstractPollingEndpoint` to cancel its currently scheduled task to avoid possible pollution with unexpected (and lost) messages **Cherry-pick to `6.2.x`**
1 parent 40d4150 commit 11e0ea1

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-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.
@@ -33,6 +33,7 @@
3333
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3434
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
3535
import org.springframework.beans.factory.support.RootBeanDefinition;
36+
import org.springframework.context.SmartLifecycle;
3637
import org.springframework.core.Ordered;
3738
import org.springframework.core.log.LogAccessor;
3839
import org.springframework.integration.channel.ChannelUtils;
@@ -276,6 +277,7 @@ private void registerTaskScheduler() {
276277
IntegrationProperties.TASK_SCHEDULER_POOL_SIZE))
277278
.addPropertyValue("threadNamePrefix", "task-scheduler-")
278279
.addPropertyValue("rejectedExecutionHandler", new CallerRunsPolicy())
280+
.addPropertyValue("phase", SmartLifecycle.DEFAULT_PHASE / 2)
279281
.addPropertyReference("errorHandler",
280282
ChannelUtils.MESSAGE_PUBLISHING_ERROR_HANDLER_BEAN_NAME)
281283
.getBeanDefinition();

spring-integration-core/src/test/java/org/springframework/integration/endpoint/PollingLifecycleTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-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.
@@ -29,18 +29,21 @@
2929
import org.springframework.beans.factory.BeanFactory;
3030
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
3131
import org.springframework.context.Lifecycle;
32+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3233
import org.springframework.integration.channel.NullChannel;
3334
import org.springframework.integration.channel.QueueChannel;
3435
import org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean;
3536
import org.springframework.integration.config.TestErrorHandler;
3637
import org.springframework.integration.core.MessageSource;
3738
import org.springframework.integration.scheduling.PollerMetadata;
39+
import org.springframework.integration.util.TestDefaultAnnotationConfiguration;
3840
import org.springframework.messaging.Message;
3941
import org.springframework.messaging.MessageHandler;
4042
import org.springframework.messaging.MessagingException;
4143
import org.springframework.messaging.support.GenericMessage;
4244
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
4345
import org.springframework.scheduling.support.PeriodicTrigger;
46+
import org.springframework.util.StopWatch;
4447

4548
import static org.assertj.core.api.Assertions.assertThat;
4649
import static org.mockito.Mockito.atMost;
@@ -220,4 +223,24 @@ public boolean isRunning() {
220223
assertThat(stopInvoked.get()).isTrue();
221224
}
222225

226+
@Test
227+
public void theScheduledPollingTaskIsCancelledNotCausingApplicationContextStopDeadLock() {
228+
var context = new AnnotationConfigApplicationContext();
229+
context.register(TestDefaultAnnotationConfiguration.class);
230+
231+
PollingConsumer consumer = new PollingConsumer(new QueueChannel(), (m) -> { });
232+
consumer.setTrigger(new PeriodicTrigger(Duration.ofSeconds(10)));
233+
consumer.setReceiveTimeout(30_000);
234+
235+
context.registerBean(PollingConsumer.class, () -> consumer);
236+
context.refresh();
237+
238+
StopWatch stopWatch = new StopWatch();
239+
stopWatch.start();
240+
context.close();
241+
stopWatch.stop();
242+
243+
assertThat(stopWatch.getTotalTimeMillis()).isLessThan(10_000);
244+
}
245+
223246
}

spring-integration-core/src/test/java/org/springframework/integration/scattergather/config/ScatterGatherTests-context.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
<service-activator input-channel="serviceChannel1" expression="T(java.lang.Math).random() * 10"/>
3030

3131
<!--Distribution scenario-->
32-
<scatter-gather input-channel="inputDistribution" output-channel="output" gather-channel="gatherChannel">
32+
<scatter-gather input-channel="inputDistribution" output-channel="output" gather-channel="gatherChannel"
33+
gather-timeout="1000">
3334
<scatterer>
3435
<recipient channel="distribution1Channel"/>
3536
<recipient channel="distribution2Channel"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.util;
18+
19+
import org.springframework.context.annotation.Configuration;
20+
import org.springframework.integration.config.EnableIntegration;
21+
22+
@Configuration
23+
@EnableIntegration
24+
public class TestDefaultAnnotationConfiguration {
25+
}

0 commit comments

Comments
 (0)