Skip to content

Commit 3b330ae

Browse files
wilkinsonaphilwebb
authored andcommitted
Shut down management server once main server's shut down
Closes gh-41002
1 parent 2a64cf6 commit 3b330ae

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-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.
@@ -43,16 +43,15 @@
4343
import org.springframework.context.SmartLifecycle;
4444
import org.springframework.context.annotation.AnnotationConfigRegistry;
4545
import org.springframework.context.aot.ApplicationContextAotGenerator;
46-
import org.springframework.context.event.ContextClosedEvent;
4746
import org.springframework.context.support.AbstractApplicationContext;
4847
import org.springframework.context.support.GenericApplicationContext;
4948
import org.springframework.core.io.DefaultResourceLoader;
5049
import org.springframework.javapoet.ClassName;
5150
import org.springframework.util.Assert;
5251

5352
/**
54-
* {@link ApplicationListener} used to initialize the management context when it's running
55-
* on a different port.
53+
* {@link SmartLifecycle} used to initialize the management context when it's running on a
54+
* different port.
5655
*
5756
* @author Andy Wilkinson
5857
* @author Phillip Webb
@@ -61,20 +60,20 @@ class ChildManagementContextInitializer implements BeanRegistrationAotProcessor,
6160

6261
private final ManagementContextFactory managementContextFactory;
6362

64-
private final ApplicationContext parentContext;
63+
private final AbstractApplicationContext parentContext;
6564

6665
private final ApplicationContextInitializer<ConfigurableApplicationContext> applicationContextInitializer;
6766

6867
private volatile ConfigurableApplicationContext managementContext;
6968

7069
ChildManagementContextInitializer(ManagementContextFactory managementContextFactory,
71-
ApplicationContext parentContext) {
70+
AbstractApplicationContext parentContext) {
7271
this(managementContextFactory, parentContext, null);
7372
}
7473

7574
@SuppressWarnings("unchecked")
7675
private ChildManagementContextInitializer(ManagementContextFactory managementContextFactory,
77-
ApplicationContext parentContext,
76+
AbstractApplicationContext parentContext,
7877
ApplicationContextInitializer<? extends ConfigurableApplicationContext> applicationContextInitializer) {
7978
this.managementContextFactory = managementContextFactory;
8079
this.parentContext = parentContext;
@@ -100,7 +99,12 @@ public void start() {
10099
@Override
101100
public void stop() {
102101
if (this.managementContext != null) {
103-
this.managementContext.stop();
102+
if (this.parentContext.isClosed()) {
103+
this.managementContext.close();
104+
}
105+
else {
106+
this.managementContext.stop();
107+
}
104108
}
105109
}
106110

@@ -111,7 +115,7 @@ public boolean isRunning() {
111115

112116
@Override
113117
public int getPhase() {
114-
return WebServerGracefulShutdownLifecycle.SMART_LIFECYCLE_PHASE + 512;
118+
return WebServerGracefulShutdownLifecycle.SMART_LIFECYCLE_PHASE - 512;
115119
}
116120

117121
@Override
@@ -161,8 +165,7 @@ protected final ConfigurableApplicationContext createManagementContext() {
161165
}
162166

163167
private boolean isLazyInitialization() {
164-
AbstractApplicationContext context = (AbstractApplicationContext) this.parentContext;
165-
List<BeanFactoryPostProcessor> postProcessors = context.getBeanFactoryPostProcessors();
168+
List<BeanFactoryPostProcessor> postProcessors = this.parentContext.getBeanFactoryPostProcessors();
166169
return postProcessors.stream().anyMatch(LazyInitializationBeanFactoryPostProcessor.class::isInstance);
167170
}
168171

@@ -205,8 +208,8 @@ public void applyTo(GenerationContext generationContext, BeanRegistrationCode be
205208
}
206209

207210
/**
208-
* {@link ApplicationListener} to propagate the {@link ContextClosedEvent} and
209-
* {@link ApplicationFailedEvent} from a parent to a child.
211+
* {@link ApplicationListener} to propagate the {@link ApplicationFailedEvent} from a
212+
* parent to a child.
210213
*/
211214
private static class CloseManagementContextListener implements ApplicationListener<ApplicationEvent> {
212215

@@ -221,18 +224,11 @@ private static class CloseManagementContextListener implements ApplicationListen
221224

222225
@Override
223226
public void onApplicationEvent(ApplicationEvent event) {
224-
if (event instanceof ContextClosedEvent contextClosedEvent) {
225-
onContextClosedEvent(contextClosedEvent);
226-
}
227227
if (event instanceof ApplicationFailedEvent applicationFailedEvent) {
228228
onApplicationFailedEvent(applicationFailedEvent);
229229
}
230230
}
231231

232-
private void onContextClosedEvent(ContextClosedEvent event) {
233-
propagateCloseIfNecessary(event.getApplicationContext());
234-
}
235-
236232
private void onApplicationFailedEvent(ApplicationFailedEvent event) {
237233
propagateCloseIfNecessary(event.getApplicationContext());
238234
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-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.
@@ -24,9 +24,9 @@
2424
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
2525
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2626
import org.springframework.boot.context.properties.EnableConfigurationProperties;
27-
import org.springframework.context.ApplicationContext;
2827
import org.springframework.context.annotation.Bean;
2928
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.context.support.AbstractApplicationContext;
3030
import org.springframework.core.Ordered;
3131
import org.springframework.core.env.ConfigurableEnvironment;
3232
import org.springframework.core.env.Environment;
@@ -112,7 +112,7 @@ static class DifferentManagementContextConfiguration {
112112

113113
@Bean
114114
static ChildManagementContextInitializer childManagementContextInitializer(
115-
ManagementContextFactory managementContextFactory, ApplicationContext parentContext) {
115+
ManagementContextFactory managementContextFactory, AbstractApplicationContext parentContext) {
116116
return new ChildManagementContextInitializer(managementContextFactory, parentContext);
117117
}
118118

0 commit comments

Comments
 (0)