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
This commit is contained in:
Phillip Webb 2015-10-15 17:52:51 -07:00
parent 84305825e7
commit 7e99d08473
2 changed files with 43 additions and 24 deletions

View File

@ -52,7 +52,6 @@ import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfigurat
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@ -141,11 +140,17 @@ public class EndpointWebMvcAutoConfiguration
managementPort = ManagementServerPort
.get(this.applicationContext.getEnvironment(), this.beanFactory);
}
if (managementPort == ManagementServerPort.DIFFERENT
&& this.applicationContext instanceof EmbeddedWebApplicationContext
&& ((EmbeddedWebApplicationContext) this.applicationContext)
.getEmbeddedServletContainer() != null) {
createChildManagementContext();
if (managementPort == ManagementServerPort.DIFFERENT) {
if (this.applicationContext instanceof EmbeddedWebApplicationContext
&& ((EmbeddedWebApplicationContext) this.applicationContext)
.getEmbeddedServletContainer() != null) {
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
.getEnvironment() instanceof ConfigurableEnvironment) {
@ -165,24 +170,8 @@ public class EndpointWebMvcAutoConfiguration
DispatcherServletAutoConfiguration.class);
CloseEventPropagationListener.addIfPossible(this.applicationContext,
childContext);
try {
childContext.refresh();
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;
}
}
childContext.refresh();
managementContextResolver().setApplicationContext(childContext);
}
/**

View File

@ -17,6 +17,8 @@
package org.springframework.boot.actuate.autoconfigure;
import java.io.FileNotFoundException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketException;
import java.net.URI;
import java.nio.charset.Charset;
@ -30,7 +32,9 @@ import javax.servlet.http.HttpServletResponse;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;
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.ConfigurableEmbeddedServletContainer;
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.web.ServerPortInfoApplicationContextInitializer;
import org.springframework.boot.test.EnvironmentTestUtils;
@ -95,6 +100,9 @@ import static org.mockito.Mockito.mock;
*/
public class EndpointWebMvcAutoConfigurationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private final AnnotationConfigEmbeddedWebApplicationContext applicationContext = new AnnotationConfigEmbeddedWebApplicationContext();
private static ThreadLocal<Ports> ports = new ThreadLocal<Ports>();
@ -234,6 +242,28 @@ public class EndpointWebMvcAutoConfigurationTests {
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
public void contextPath() throws Exception {
EnvironmentTestUtils.addEnvironment(this.applicationContext,