Fail startup if management server can't start

Update EndpointWebMvcAutoConfiguration to no longer catch and ignore
EmbeddedServletContainerExceptions. Since commit 764e34b9, starting a
management on a different port is not even attempted when running in a
classic servlet container. This means that the catch/log logic (which
was originally added in 45315a97) is no longer necessary, and only
serves to hide genuine problems.

Fixes gh-4064
pull/4207/head
Phillip Webb 9 years ago
parent 84305825e7
commit 7e99d08473

@ -52,7 +52,6 @@ import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfigurat
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
@ -141,11 +140,17 @@ public class EndpointWebMvcAutoConfiguration
managementPort = ManagementServerPort managementPort = ManagementServerPort
.get(this.applicationContext.getEnvironment(), this.beanFactory); .get(this.applicationContext.getEnvironment(), this.beanFactory);
} }
if (managementPort == ManagementServerPort.DIFFERENT if (managementPort == ManagementServerPort.DIFFERENT) {
&& this.applicationContext instanceof EmbeddedWebApplicationContext if (this.applicationContext instanceof EmbeddedWebApplicationContext
&& ((EmbeddedWebApplicationContext) this.applicationContext) && ((EmbeddedWebApplicationContext) this.applicationContext)
.getEmbeddedServletContainer() != null) { .getEmbeddedServletContainer() != null) {
createChildManagementContext(); createChildManagementContext();
}
else {
logger.warn("Could not start embedded management container on "
+ "different port (management endpoints are still available "
+ "through JMX)");
}
} }
if (managementPort == ManagementServerPort.SAME && this.applicationContext if (managementPort == ManagementServerPort.SAME && this.applicationContext
.getEnvironment() instanceof ConfigurableEnvironment) { .getEnvironment() instanceof ConfigurableEnvironment) {
@ -165,24 +170,8 @@ public class EndpointWebMvcAutoConfiguration
DispatcherServletAutoConfiguration.class); DispatcherServletAutoConfiguration.class);
CloseEventPropagationListener.addIfPossible(this.applicationContext, CloseEventPropagationListener.addIfPossible(this.applicationContext,
childContext); childContext);
try { childContext.refresh();
childContext.refresh(); managementContextResolver().setApplicationContext(childContext);
managementContextResolver().setApplicationContext(childContext);
}
catch (RuntimeException ex) {
// No support currently for deploying a war with management.port=<different>,
// and this is the signature of that happening
if (ex instanceof EmbeddedServletContainerException
|| ex.getCause() instanceof EmbeddedServletContainerException) {
logger.warn(
"Could not start embedded management container (management endpoints "
+ "are still available through JMX)");
logger.debug("Embedded management container startup failed", ex);
}
else {
throw ex;
}
}
} }
/** /**

@ -17,6 +17,8 @@
package org.springframework.boot.actuate.autoconfigure; package org.springframework.boot.actuate.autoconfigure;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketException; import java.net.SocketException;
import java.net.URI; import java.net.URI;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -30,7 +32,9 @@ import javax.servlet.http.HttpServletResponse;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping; import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMappingCustomizer; import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMappingCustomizer;
@ -51,6 +55,7 @@ import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent; import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer; import org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
@ -95,6 +100,9 @@ import static org.mockito.Mockito.mock;
*/ */
public class EndpointWebMvcAutoConfigurationTests { public class EndpointWebMvcAutoConfigurationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private final AnnotationConfigEmbeddedWebApplicationContext applicationContext = new AnnotationConfigEmbeddedWebApplicationContext(); private final AnnotationConfigEmbeddedWebApplicationContext applicationContext = new AnnotationConfigEmbeddedWebApplicationContext();
private static ThreadLocal<Ports> ports = new ThreadLocal<Ports>(); private static ThreadLocal<Ports> ports = new ThreadLocal<Ports>();
@ -234,6 +242,28 @@ public class EndpointWebMvcAutoConfigurationTests {
assertAllClosed(); assertAllClosed();
} }
@Test
public void specificPortsViaPropertiesWithClash() throws Exception {
int managementPort = ports.get().management;
ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(managementPort));
try {
EnvironmentTestUtils.addEnvironment(this.applicationContext,
"server.port:" + ports.get().server,
"management.port:" + ports.get().management);
this.applicationContext.register(RootConfig.class, EndpointConfig.class,
BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class,
ErrorMvcAutoConfiguration.class);
this.thrown.expect(EmbeddedServletContainerException.class);
this.applicationContext.refresh();
this.applicationContext.close();
}
finally {
serverSocket.close();
assertAllClosed();
}
}
@Test @Test
public void contextPath() throws Exception { public void contextPath() throws Exception {
EnvironmentTestUtils.addEnvironment(this.applicationContext, EnvironmentTestUtils.addEnvironment(this.applicationContext,

Loading…
Cancel
Save