From cb9c16a8a27548661cbd5e1070c7f143dc5ac313 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 24 Jun 2024 12:20:12 +0100 Subject: [PATCH] WIP --- .../ChildManagementContextInitializer.java | 31 +++++++++---------- .../ManagementContextAutoConfiguration.java | 4 +-- .../src/main/resources/application.properties | 1 + 3 files changed, 18 insertions(+), 18 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 e580d5dfcd1c..7f94490a0685 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 @@ -43,7 +43,6 @@ 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; @@ -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; @@ -83,6 +82,7 @@ private ChildManagementContextInitializer(ManagementContextFactory managementCon @Override public void start() { + System.out.println("Starting child management context"); if (!(this.parentContext instanceof WebServerApplicationContext)) { return; } @@ -100,7 +100,14 @@ public void start() { @Override public void stop() { if (this.managementContext != null) { - this.managementContext.stop(); + if (this.parentContext.isClosed()) { + System.out.println("Stopping child management context"); + this.managementContext.stop(); + } + else { + System.out.println("Closing child management context"); + this.managementContext.close(); + } } } @@ -161,8 +168,7 @@ protected final ConfigurableApplicationContext createManagementContext() { } 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 +211,8 @@ public void applyTo(GenerationContext generationContext, BeanRegistrationCode be } /** - * {@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 +227,11 @@ private static class CloseManagementContextListener implements ApplicationListen @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 da3da45ad2bc..938afa10c07f 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 @@ -24,9 +24,9 @@ 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 @@ static class DifferentManagementContextConfiguration { @Bean static ChildManagementContextInitializer childManagementContextInitializer( - ManagementContextFactory managementContextFactory, ApplicationContext parentContext) { + ManagementContextFactory managementContextFactory, AbstractApplicationContext parentContext) { return new ChildManagementContextInitializer(managementContextFactory, parentContext); } diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/main/resources/application.properties b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/main/resources/application.properties index 2c35d22ff033..b699dd9b3e83 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/main/resources/application.properties +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/main/resources/application.properties @@ -9,6 +9,7 @@ spring.security.user.password=password management.endpoints.web.exposure.include=* management.endpoint.shutdown.enabled=true +management.server.port=8081 server.tomcat.basedir=target/tomcat server.tomcat.accesslog.enabled=true