Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Jun 24, 2024
1 parent 6bdba8e commit cb9c16a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -61,20 +60,20 @@ class ChildManagementContextInitializer implements BeanRegistrationAotProcessor,

private final ManagementContextFactory managementContextFactory;

private final ApplicationContext parentContext;
private final AbstractApplicationContext parentContext;

private final ApplicationContextInitializer<ConfigurableApplicationContext> 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<? extends ConfigurableApplicationContext> applicationContextInitializer) {
this.managementContextFactory = managementContextFactory;
this.parentContext = parentContext;
Expand All @@ -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;
}
Expand All @@ -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();
}
}
}

Expand Down Expand Up @@ -161,8 +168,7 @@ protected final ConfigurableApplicationContext createManagementContext() {
}

private boolean isLazyInitialization() {
AbstractApplicationContext context = (AbstractApplicationContext) this.parentContext;
List<BeanFactoryPostProcessor> postProcessors = context.getBeanFactoryPostProcessors();
List<BeanFactoryPostProcessor> postProcessors = this.parentContext.getBeanFactoryPostProcessors();
return postProcessors.stream().anyMatch(LazyInitializationBeanFactoryPostProcessor.class::isInstance);
}

Expand Down Expand Up @@ -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<ApplicationEvent> {

Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -112,7 +112,7 @@ static class DifferentManagementContextConfiguration {

@Bean
static ChildManagementContextInitializer childManagementContextInitializer(
ManagementContextFactory managementContextFactory, ApplicationContext parentContext) {
ManagementContextFactory managementContextFactory, AbstractApplicationContext parentContext) {
return new ChildManagementContextInitializer(managementContextFactory, parentContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cb9c16a

Please sign in to comment.