Skip to content

Commit a518707

Browse files
committed
GH-371 - Disable waiting for termination defaulting on Spring Framework 6.1.
We now skip the defaulting to wait of the pool shutdown termination on Spring Framework 6.1, as on that, pools implement Lifecycle and we need to wait for complete shutdown during Lifecycle.stop() already and setting the flag would delay that wait to the actual destruction phase.
1 parent 3692a20 commit a518707

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/config/EventPublicationAutoConfiguration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3333
import org.springframework.boot.autoconfigure.task.TaskExecutionProperties;
3434
import org.springframework.boot.autoconfigure.task.TaskExecutionProperties.Shutdown;
35+
import org.springframework.context.Lifecycle;
3536
import org.springframework.context.annotation.Bean;
3637
import org.springframework.context.annotation.Import;
3738
import org.springframework.context.annotation.Role;
@@ -44,6 +45,7 @@
4445
import org.springframework.modulith.events.support.PersistentApplicationEventMulticaster;
4546
import org.springframework.scheduling.annotation.AbstractAsyncConfiguration;
4647
import org.springframework.scheduling.annotation.EnableAsync;
48+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
4749

4850
/**
4951
* Fundamental configuration for the {@link EventPublicationRegistry} support.
@@ -100,6 +102,9 @@ static class AsyncPropertiesDefaulter implements BeanPostProcessor {
100102
private static final Logger LOGGER = LoggerFactory.getLogger(AsyncPropertiesDefaulter.class);
101103
private static final String PROPERTY = "spring.task.execution.shutdown.await-termination";
102104

105+
private static final boolean IS_SPRING_6_1_OR_BETTER = Lifecycle.class
106+
.isAssignableFrom(ThreadPoolTaskExecutor.class);
107+
103108
private final Environment environment;
104109

105110
AsyncPropertiesDefaulter(Environment environment) {
@@ -114,6 +119,10 @@ static class AsyncPropertiesDefaulter implements BeanPostProcessor {
114119
@Override
115120
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
116121

122+
if (IS_SPRING_6_1_OR_BETTER) {
123+
return bean;
124+
}
125+
117126
if (!(bean instanceof TaskExecutionProperties p)) {
118127
return bean;
119128
}

spring-modulith-events/spring-modulith-events-core/src/test/java/org/springframework/modulith/events/config/EventPublicationAutoConfigurationIntegrationTests.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3636
import org.springframework.boot.test.context.runner.ContextConsumer;
3737
import org.springframework.context.annotation.AdviceMode;
38+
import org.springframework.lang.Nullable;
3839
import org.springframework.modulith.events.CompletedEventPublications;
3940
import org.springframework.modulith.events.IncompleteEventPublications;
4041
import org.springframework.modulith.events.config.EventPublicationAutoConfiguration.AsyncPropertiesDefaulter;
@@ -55,17 +56,16 @@ class EventPublicationAutoConfigurationIntegrationTests {
5556

5657
@Mock EventPublicationRepository repository;
5758

58-
@Test // GH-149
59-
void registersAsyncTerminationDefaulterByDefault() {
59+
@Test // GH-149, GH-371
60+
void doesNotRegisterAsyncTerminationDefaulterByDefault() {
6061

61-
basicSetup()
62-
.run(context -> {
63-
assertThat(context).hasSingleBean(AsyncPropertiesDefaulter.class);
62+
basicSetup().run(context -> {
6463

65-
expect(Shutdown::isAwaitTermination, true).andThen(
66-
expect(Shutdown::getAwaitTerminationPeriod, Duration.ofSeconds(2)))
67-
.accept(context);
68-
});
64+
assertThat(context).hasSingleBean(AsyncPropertiesDefaulter.class);
65+
66+
expect(Shutdown::isAwaitTermination, false);
67+
expect(Shutdown::getAwaitTerminationPeriod, null);
68+
});
6969
}
7070

7171
@Test // GH-149
@@ -137,8 +137,8 @@ void exposesCompletedAndIncompleteEventPublications() {
137137
});
138138
}
139139

140-
private <T> ContextConsumer<AssertableApplicationContext> expect(Function<Shutdown, T> extractor,
141-
T expected) {
140+
private static <T> ContextConsumer<AssertableApplicationContext> expect(Function<Shutdown, T> extractor,
141+
@Nullable T expected) {
142142

143143
return context -> assertThat(context.getBean(TaskExecutionProperties.class).getShutdown())
144144
.extracting(extractor)

0 commit comments

Comments
 (0)