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`**

(cherry picked from commit 11e0ea1305)
This commit is contained in:
Artem Bilan
2024-01-12 13:51:45 -05:00
parent ab3516cb86
commit 0be2c76530
4 changed files with 54 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.SmartLifecycle;
import org.springframework.core.Ordered;
import org.springframework.core.log.LogAccessor;
import org.springframework.integration.channel.ChannelUtils;
@@ -276,6 +277,7 @@ public class DefaultConfiguringBeanFactoryPostProcessor implements BeanDefinitio
IntegrationProperties.TASK_SCHEDULER_POOL_SIZE))
.addPropertyValue("threadNamePrefix", "task-scheduler-")
.addPropertyValue("rejectedExecutionHandler", new CallerRunsPolicy())
.addPropertyValue("phase", SmartLifecycle.DEFAULT_PHASE / 2)
.addPropertyReference("errorHandler",
ChannelUtils.MESSAGE_PUBLISHING_ERROR_HANDLER_BEAN_NAME)
.getBeanDefinition();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,18 +29,21 @@ import org.mockito.Mockito;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.Lifecycle;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.integration.channel.NullChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean;
import org.springframework.integration.config.TestErrorHandler;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.integration.util.TestDefaultAnnotationConfiguration;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.util.StopWatch;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.atMost;
@@ -220,4 +223,24 @@ public class PollingLifecycleTests {
assertThat(stopInvoked.get()).isTrue();
}
@Test
public void theScheduledPollingTaskIsCancelledNotCausingApplicationContextStopDeadLock() {
var context = new AnnotationConfigApplicationContext();
context.register(TestDefaultAnnotationConfiguration.class);
PollingConsumer consumer = new PollingConsumer(new QueueChannel(), (m) -> { });
consumer.setTrigger(new PeriodicTrigger(Duration.ofSeconds(10)));
consumer.setReceiveTimeout(30_000);
context.registerBean(PollingConsumer.class, () -> consumer);
context.refresh();
StopWatch stopWatch = new StopWatch();
stopWatch.start();
context.close();
stopWatch.stop();
assertThat(stopWatch.getTotalTimeMillis()).isLessThan(10_000);
}
}

View File

@@ -29,7 +29,8 @@
<service-activator input-channel="serviceChannel1" expression="T(java.lang.Math).random() * 10"/>
<!--Distribution scenario-->
<scatter-gather input-channel="inputDistribution" output-channel="output" gather-channel="gatherChannel">
<scatter-gather input-channel="inputDistribution" output-channel="output" gather-channel="gatherChannel"
gather-timeout="1000">
<scatterer>
<recipient channel="distribution1Channel"/>
<recipient channel="distribution2Channel"/>

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.util;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.config.EnableIntegration;
@Configuration
@EnableIntegration
public class TestDefaultAnnotationConfiguration {
}