Skip to content

Commit 1a3c25b

Browse files
committed
Fix phase for TaskScheduler instances in tests
Related to: #8856 Many tests create their own `ThreadPoolTaskScheduler` beans. Therefore, its default phase might affect the memory and performance. * Use `phase = SmartLifecycle.DEFAULT_PHASE / 2` for manual `ThreadPoolTaskScheduler` beans * Migrate affected tests classes to JUnit 5 * Make some other configuration adjustments for better performance **Cherry-pick to `6.2.x`** (cherry picked from commit 39c99c0)
1 parent 0be2c76 commit 1a3c25b

File tree

25 files changed

+246
-202
lines changed

25 files changed

+246
-202
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans:beans xmlns="http://www.springframework.org/schema/integration"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xmlns:beans="http://www.springframework.org/schema/beans"
5-
xmlns:task="http://www.springframework.org/schema/task"
6-
xmlns:context="http://www.springframework.org/schema/context"
7-
xmlns:util="http://www.springframework.org/schema/util"
8-
xsi:schemaLocation="http://www.springframework.org/schema/task https://www.springframework.org/schema/task/spring-task.xsd
9-
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:beans="http://www.springframework.org/schema/beans"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xmlns:util="http://www.springframework.org/schema/util"
7+
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
108
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
119
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
1210
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
1311

1412
<context:property-placeholder properties-ref="props"/>
1513

16-
<util:properties id="props"/>
14+
<util:properties id="props"/>
1715

1816
<channel id="outputChannel">
1917
<queue capacity="5"/>
@@ -40,96 +38,99 @@
4038
<beans:bean id="transactionManager" class="org.springframework.integration.transaction.PseudoTransactionManager"/>
4139

4240
<aggregator id="completelyDefinedAggregator"
43-
input-channel="completelyDefinedAggregatorInput"
44-
output-channel="outputChannel"
45-
discard-channel="discardChannel"
46-
ref="aggregatorBean"
47-
release-strategy="releaseStrategy"
48-
correlation-strategy="correlationStrategy"
49-
send-timeout="86420000"
50-
send-partial-result-on-expiry="true"
51-
expire-groups-upon-completion="true"
52-
expire-groups-upon-timeout="false"
53-
empty-group-min-timeout="123"
54-
group-timeout="456"
55-
lock-registry="lockRegistry"
56-
scheduler="scheduler"
57-
message-store="store"
58-
pop-sequence="false"
59-
order="5"
60-
expire-duration="10000"
61-
expire-timeout="250">
62-
<expire-transactional/>
41+
input-channel="completelyDefinedAggregatorInput"
42+
output-channel="outputChannel"
43+
discard-channel="discardChannel"
44+
ref="aggregatorBean"
45+
release-strategy="releaseStrategy"
46+
correlation-strategy="correlationStrategy"
47+
send-timeout="86420000"
48+
send-partial-result-on-expiry="true"
49+
expire-groups-upon-completion="true"
50+
expire-groups-upon-timeout="false"
51+
empty-group-min-timeout="123"
52+
group-timeout="456"
53+
lock-registry="lockRegistry"
54+
scheduler="scheduler"
55+
message-store="store"
56+
pop-sequence="false"
57+
order="5"
58+
expire-duration="10000"
59+
expire-timeout="250">
60+
<expire-transactional/>
6361
</aggregator>
6462

6563
<beans:bean id="lockRegistry" class="org.springframework.integration.support.locks.DefaultLockRegistry"/>
6664

67-
<task:scheduler id="scheduler"/>
65+
<beans:bean id="scheduler"
66+
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
67+
<beans:property name="phase" value="1073741823"/>
68+
</beans:bean>
6869

6970
<beans:bean id="store" class="org.springframework.integration.store.SimpleMessageStore"/>
7071

7172
<channel id="aggregatorWithExpressionsInput"/>
7273
<channel id="aggregatorWithExpressionsOutput"/>
7374
<aggregator id="aggregatorWithExpressions"
74-
input-channel="aggregatorWithExpressionsInput"
75-
output-channel="aggregatorWithExpressionsOutput"
76-
expression="?[payload.startsWith('1')].![payload]"
77-
release-strategy-expression="#root.size()>2"
78-
correlation-strategy-expression="headers['foo']"/>
75+
input-channel="aggregatorWithExpressionsInput"
76+
output-channel="aggregatorWithExpressionsOutput"
77+
expression="?[payload.startsWith('1')].![payload]"
78+
release-strategy-expression="#root.size()>2"
79+
correlation-strategy-expression="headers['foo']"/>
7980

8081
<channel id="aggregatorWithReferenceAndMethodInput"/>
8182
<aggregator id="aggregatorWithReferenceAndMethod"
82-
ref="adderBean"
83-
method="add"
84-
input-channel="aggregatorWithReferenceAndMethodInput"
85-
output-channel="outputChannel"/>
83+
ref="adderBean"
84+
method="add"
85+
input-channel="aggregatorWithReferenceAndMethodInput"
86+
output-channel="outputChannel"/>
8687

8788
<channel id="aggregatorWithPojoReleaseStrategyInput"/>
8889
<aggregator id="aggregatorWithPojoReleaseStrategy"
89-
input-channel="aggregatorWithPojoReleaseStrategyInput"
90-
output-channel="outputChannel"
91-
ref="adderBean"
92-
method="add"
93-
release-strategy="pojoReleaseStrategy"
94-
release-strategy-method="checkCompletenessAsList"/>
90+
input-channel="aggregatorWithPojoReleaseStrategyInput"
91+
output-channel="outputChannel"
92+
ref="adderBean"
93+
method="add"
94+
release-strategy="pojoReleaseStrategy"
95+
release-strategy-method="checkCompletenessAsList"/>
9596

9697
<channel id="aggregatorWithPojoReleaseStrategyInputAsCollection"/>
9798
<aggregator id="aggregatorWithPojoReleaseStrategyAsCollection"
98-
input-channel="aggregatorWithPojoReleaseStrategyInputAsCollection"
99-
output-channel="outputChannel"
100-
ref="adderBean"
101-
method="add"
102-
release-strategy="pojoReleaseStrategy"
103-
release-strategy-method="checkCompletenessAsCollection"/>
99+
input-channel="aggregatorWithPojoReleaseStrategyInputAsCollection"
100+
output-channel="outputChannel"
101+
ref="adderBean"
102+
method="add"
103+
release-strategy="pojoReleaseStrategy"
104+
release-strategy-method="checkCompletenessAsCollection"/>
104105

105106
<channel id="aggregatorWithExpressionsAndPojoAggregatorInput"/>
106107
<aggregator id="aggregatorWithExpressionsAndPojoAggregator"
107-
input-channel="aggregatorWithExpressionsAndPojoAggregatorInput"
108-
ref="aggregatorBean"
109-
release-strategy-expression="size() == 2"
110-
correlation-strategy-expression="headers['foo']"
111-
empty-group-min-timeout="60000"/>
108+
input-channel="aggregatorWithExpressionsAndPojoAggregatorInput"
109+
ref="aggregatorBean"
110+
release-strategy-expression="size() == 2"
111+
correlation-strategy-expression="headers['foo']"
112+
empty-group-min-timeout="60000"/>
112113

113114
<beans:bean id="aggregatorBean"
114-
class="org.springframework.integration.config.TestAggregatorBean" />
115+
class="org.springframework.integration.config.TestAggregatorBean"/>
115116

116117
<beans:bean id="aggregatorMGPBean"
117-
class="org.springframework.integration.aggregator.SimpleMessageGroupProcessor" />
118+
class="org.springframework.integration.aggregator.SimpleMessageGroupProcessor"/>
118119

119120
<beans:bean id="aggregatorCustomMGPBean"
120-
class="org.springframework.integration.config.AggregatorParserTests$MyMGP" />
121+
class="org.springframework.integration.config.AggregatorParserTests$MyMGP"/>
121122

122-
<beans:bean id="adderBean"
123-
class="org.springframework.integration.config.Adder" />
123+
<beans:bean id="adderBean"
124+
class="org.springframework.integration.config.Adder"/>
124125

125126
<beans:bean id="releaseStrategy"
126-
class="org.springframework.integration.config.TestReleaseStrategy" />
127+
class="org.springframework.integration.config.TestReleaseStrategy"/>
127128

128129
<beans:bean id="correlationStrategy" class="org.springframework.integration.config.TestCorrelationStrategy"/>
129130

130131
<beans:bean id="pojoReleaseStrategy"
131-
class="org.springframework.integration.config.MaxValueReleaseStrategy">
132-
<beans:constructor-arg value="10" />
132+
class="org.springframework.integration.config.MaxValueReleaseStrategy">
133+
<beans:constructor-arg value="10"/>
133134
</beans:bean>
134135

135136
</beans:beans>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575

7676
<beans:bean id="testScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"
7777
p:poolSize="7"
78+
p:phase="1073741823"
7879
p:waitForTasksToCompleteOnShutdown="true"/>
7980

8081
<beans:bean id="testMessageStore" class="org.springframework.integration.store.SimpleMessageStore"/>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
<beans:bean id="multiThreadScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"
5151
p:poolSize="5"
52+
p:phase="1073741823"
5253
p:waitForTasksToCompleteOnShutdown="true"/>
5354

5455
<service-activator input-channel="outputB" output-channel="outputB1" method="processMessage" ref="sampleHandler"/>

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@
2424
default-request-channel="requestChannel"
2525
proxy-default-methods="true"/>
2626

27+
<channel id="receiveChannel">
28+
<queue />
29+
</channel>
30+
2731
<gateway id="solicitResponse"
2832
service-interface="org.springframework.integration.gateway.TestService"
29-
default-reply-channel="replyChannel"
30-
default-reply-timeout="3000"/>
33+
default-reply-channel="receiveChannel"
34+
default-reply-timeout="5000"/>
3135

3236
<gateway id="requestReply"
3337
service-interface="org.springframework.integration.gateway.TestService"

spring-integration-core/src/test/java/org/springframework/integration/config/xml/GatewayParserTests.java

Lines changed: 3 additions & 3 deletions
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.
@@ -72,7 +72,7 @@
7272
* @author Gary Russell
7373
*/
7474
@SpringJUnitConfig
75-
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
75+
@DirtiesContext
7676
public class GatewayParserTests {
7777

7878
@Autowired
@@ -132,7 +132,7 @@ public void testOneWayOverride() {
132132

133133
@Test
134134
public void testSolicitResponse() {
135-
PollableChannel channel = (PollableChannel) context.getBean("replyChannel");
135+
PollableChannel channel = (PollableChannel) context.getBean("receiveChannel");
136136
channel.send(new GenericMessage<>("foo"));
137137
TestService service = (TestService) context.getBean("solicitResponse");
138138
String result = service.solicitResponse();

spring-integration-core/src/test/java/org/springframework/integration/config/xml/superParentApplicationContext.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
https://www.springframework.org/schema/integration/spring-integration.xsd">
88

99
<beans:bean id="taskScheduler"
10-
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler" />
10+
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
11+
<beans:property name="phase" value="1073741823"/>
12+
</beans:bean>
1113

1214
</beans:beans>

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

Lines changed: 6 additions & 2 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.
@@ -41,6 +41,7 @@
4141
import org.springframework.beans.factory.annotation.Qualifier;
4242
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
4343
import org.springframework.context.Lifecycle;
44+
import org.springframework.context.SmartLifecycle;
4445
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4546
import org.springframework.context.annotation.Bean;
4647
import org.springframework.context.annotation.ComponentScan;
@@ -586,6 +587,7 @@ public PollerSpec poller() {
586587
@Bean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)
587588
public TaskScheduler taskScheduler() {
588589
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
590+
threadPoolTaskScheduler.setPhase(SmartLifecycle.DEFAULT_PHASE / 2);
589591
threadPoolTaskScheduler.setPoolSize(100);
590592
return threadPoolTaskScheduler;
591593
}
@@ -925,7 +927,9 @@ public IntegrationFlow dedicatedPollingThreadFlow() {
925927

926928
@Bean
927929
public TaskScheduler dedicatedTaskScheduler() {
928-
return new ThreadPoolTaskScheduler();
930+
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
931+
threadPoolTaskScheduler.setPhase(SmartLifecycle.DEFAULT_PHASE / 2);
932+
return threadPoolTaskScheduler;
929933
}
930934

931935
@Bean

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

Lines changed: 2 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.
@@ -80,6 +80,7 @@ public void test() throws Exception {
8080
for (int i = 0; i < 3; i++) {
8181
messages.add(channel.receive(1000));
8282
}
83+
adapter.stop();
8384
scheduler.destroy();
8485
Message<?> message1 = messages.get(0);
8586
assertThat(message1.getPayload()).isEqualTo("test-1");

spring-integration-core/src/test/java/org/springframework/integration/store/MessageStoreReaperTests-context.xml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xmlns:task="http://www.springframework.org/schema/task"
5-
xmlns:int="http://www.springframework.org/schema/integration"
6-
xsi:schemaLocation="http://www.springframework.org/schema/task https://www.springframework.org/schema/task/spring-task.xsd
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:task="http://www.springframework.org/schema/task"
5+
xmlns:int="http://www.springframework.org/schema/integration"
6+
xsi:schemaLocation="http://www.springframework.org/schema/task https://www.springframework.org/schema/task/spring-task.xsd
77
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
88
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd">
99

@@ -23,7 +23,10 @@
2323
<task:scheduled ref="reaper3" method="run" fixed-rate="100"/>
2424
</task:scheduled-tasks>
2525

26-
<task:scheduler id="scheduler"/>
26+
<bean id="scheduler"
27+
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
28+
<property name="phase" value="1073741823"/>
29+
</bean>
2730

2831
<bean id="messageStore2" class="org.springframework.integration.store.SimpleMessageStore">
2932
<property name="expiryCallbacks" ref="expiryCallback2"/>
@@ -35,21 +38,21 @@
3538
<property name="messageGroupStore" ref="messageStore2"/>
3639
</bean>
3740

38-
<bean id="messageStore3" class="org.springframework.integration.store.SimpleMessageStore" />
41+
<bean id="messageStore3" class="org.springframework.integration.store.SimpleMessageStore"/>
3942

4043
<bean id="reaper3" class="org.springframework.integration.store.MessageGroupStoreReaper">
4144
<property name="messageGroupStore" ref="messageStore3"/>
42-
<property name="timeout" value="50" />
45+
<property name="timeout" value="50"/>
4346
</bean>
4447

4548
<int:aggregator input-channel="aggChannel"
46-
discard-channel="discards"
47-
message-store="messageStore3" />
49+
discard-channel="discards"
50+
message-store="messageStore3"/>
4851

49-
<int:channel id="aggChannel" />
52+
<int:channel id="aggChannel"/>
5053

5154
<int:channel id="discards">
52-
<int:queue />
55+
<int:queue/>
5356
</int:channel>
5457

5558
</beans>

spring-integration-file/src/test/java/org/springframework/integration/file/FileWritingMessageHandlerTests.java

Lines changed: 3 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.
@@ -543,6 +543,7 @@ public void noFlushAppend() throws Exception {
543543
}
544544
assertThat(flushes.get()).isGreaterThanOrEqualTo(2);
545545
handler.stop();
546+
taskScheduler.destroy();
546547
}
547548

548549
@Test
@@ -586,6 +587,7 @@ protected BufferedOutputStream createOutputStream(File fileToWriteTo, boolean ap
586587
verify(out).write(any(byte[].class), anyInt(), anyInt());
587588
assertThat(closeWhileWriting.get()).isFalse();
588589
handler.stop();
590+
taskScheduler.destroy();
589591
}
590592

591593
@Test

0 commit comments

Comments
 (0)