Skip to content

Commit

Permalink
Shut down management server once main server's shut down
Browse files Browse the repository at this point in the history
Closes gh-41002
  • Loading branch information
wilkinsona authored and philwebb committed Oct 23, 2024
1 parent 2a64cf6 commit 3b330ae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -43,16 +43,15 @@
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;
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
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 @@ -100,7 +99,12 @@ public void start() {
@Override
public void stop() {
if (this.managementContext != null) {
this.managementContext.stop();
if (this.parentContext.isClosed()) {
this.managementContext.close();
}
else {
this.managementContext.stop();
}
}
}

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

@Override
public int getPhase() {
return WebServerGracefulShutdownLifecycle.SMART_LIFECYCLE_PHASE + 512;
return WebServerGracefulShutdownLifecycle.SMART_LIFECYCLE_PHASE - 512;
}

@Override
Expand Down Expand Up @@ -161,8 +165,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 +208,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 +224,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
@@ -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.
Expand All @@ -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

0 comments on commit 3b330ae

Please sign in to comment.