From 3b330ae09a8d09a32ca956e238f10835bd317b00 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 2 Oct 2024 14:55:09 +0100 Subject: [PATCH] Shut down management server once main server's shut down Closes gh-41002 --- .../ChildManagementContextInitializer.java | 36 +++++++++---------- .../ManagementContextAutoConfiguration.java | 6 ++-- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java index e580d5dfcd..21de0fcad0 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-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. @@ -43,7 +43,6 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.SmartLifecycle; import org.springframework.context.annotation.AnnotationConfigRegistry; import org.springframework.context.aot.ApplicationContextAotGenerator; -import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.io.DefaultResourceLoader; @@ -51,8 +50,8 @@ import org.springframework.javapoet.ClassName; import org.springframework.util.Assert; /** - * {@link ApplicationListener} used to initialize the management context when it's running - * on a different port. + * {@link SmartLifecycle} used to initialize the management context when it's running on a + * different port. * * @author Andy Wilkinson * @author Phillip Webb @@ -61,20 +60,20 @@ class ChildManagementContextInitializer implements BeanRegistrationAotProcessor, private final ManagementContextFactory managementContextFactory; - private final ApplicationContext parentContext; + private final AbstractApplicationContext parentContext; private final ApplicationContextInitializer applicationContextInitializer; private volatile ConfigurableApplicationContext managementContext; ChildManagementContextInitializer(ManagementContextFactory managementContextFactory, - ApplicationContext parentContext) { + AbstractApplicationContext parentContext) { this(managementContextFactory, parentContext, null); } @SuppressWarnings("unchecked") private ChildManagementContextInitializer(ManagementContextFactory managementContextFactory, - ApplicationContext parentContext, + AbstractApplicationContext parentContext, ApplicationContextInitializer applicationContextInitializer) { this.managementContextFactory = managementContextFactory; this.parentContext = parentContext; @@ -100,7 +99,12 @@ class ChildManagementContextInitializer implements BeanRegistrationAotProcessor, @Override public void stop() { if (this.managementContext != null) { - this.managementContext.stop(); + if (this.parentContext.isClosed()) { + this.managementContext.close(); + } + else { + this.managementContext.stop(); + } } } @@ -111,7 +115,7 @@ class ChildManagementContextInitializer implements BeanRegistrationAotProcessor, @Override public int getPhase() { - return WebServerGracefulShutdownLifecycle.SMART_LIFECYCLE_PHASE + 512; + return WebServerGracefulShutdownLifecycle.SMART_LIFECYCLE_PHASE - 512; } @Override @@ -161,8 +165,7 @@ class ChildManagementContextInitializer implements BeanRegistrationAotProcessor, } private boolean isLazyInitialization() { - AbstractApplicationContext context = (AbstractApplicationContext) this.parentContext; - List postProcessors = context.getBeanFactoryPostProcessors(); + List postProcessors = this.parentContext.getBeanFactoryPostProcessors(); return postProcessors.stream().anyMatch(LazyInitializationBeanFactoryPostProcessor.class::isInstance); } @@ -205,8 +208,8 @@ class ChildManagementContextInitializer implements BeanRegistrationAotProcessor, } /** - * {@link ApplicationListener} to propagate the {@link ContextClosedEvent} and - * {@link ApplicationFailedEvent} from a parent to a child. + * {@link ApplicationListener} to propagate the {@link ApplicationFailedEvent} from a + * parent to a child. */ private static class CloseManagementContextListener implements ApplicationListener { @@ -221,18 +224,11 @@ class ChildManagementContextInitializer implements BeanRegistrationAotProcessor, @Override public void onApplicationEvent(ApplicationEvent event) { - if (event instanceof ContextClosedEvent contextClosedEvent) { - onContextClosedEvent(contextClosedEvent); - } if (event instanceof ApplicationFailedEvent applicationFailedEvent) { onApplicationFailedEvent(applicationFailedEvent); } } - private void onContextClosedEvent(ContextClosedEvent event) { - propagateCloseIfNecessary(event.getApplicationContext()); - } - private void onApplicationFailedEvent(ApplicationFailedEvent event) { propagateCloseIfNecessary(event.getApplicationContext()); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java index da3da45ad2..24b8de40e4 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-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. @@ -24,9 +24,9 @@ import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureOrder; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.AbstractApplicationContext; import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; @@ -112,7 +112,7 @@ public class ManagementContextAutoConfiguration { @Bean static ChildManagementContextInitializer childManagementContextInitializer( - ManagementContextFactory managementContextFactory, ApplicationContext parentContext) { + ManagementContextFactory managementContextFactory, AbstractApplicationContext parentContext) { return new ChildManagementContextInitializer(managementContextFactory, parentContext); }