Detect Tomcat start failures

Detect if the tomcat container fails to start and propagate an
exception to the caller.
This commit is contained in:
Phillip Webb 2013-11-05 16:50:29 -08:00
parent 2910fe618b
commit 4c75700164
3 changed files with 14 additions and 3 deletions

View File

@ -24,7 +24,6 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.TestUtils; import org.springframework.boot.TestUtils;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
@ -35,6 +34,7 @@ import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.properties.ServerProperties; import org.springframework.boot.context.embedded.properties.ServerProperties;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -133,7 +133,7 @@ public class ServerPropertiesAutoConfigurationTests {
this.context.register(Config.class, MutiServerPropertiesBeanConfig.class, this.context.register(Config.class, MutiServerPropertiesBeanConfig.class,
ServerPropertiesAutoConfiguration.class, ServerPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.thrown.expect(BeanCreationException.class); this.thrown.expect(ApplicationContextException.class);
this.thrown.expectMessage("Multiple ServerProperties"); this.thrown.expectMessage("Multiple ServerProperties");
this.context.refresh(); this.context.refresh();
} }

View File

@ -131,8 +131,14 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
protected void onRefresh() { protected void onRefresh() {
super.onRefresh(); super.onRefresh();
registerShutdownHook(); registerShutdownHook();
try {
createEmbeddedServletContainer(); createEmbeddedServletContainer();
} }
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start embedded container",
ex);
}
}
@Override @Override
protected void finishRefresh() { protected void finishRefresh() {

View File

@ -17,6 +17,7 @@
package org.springframework.boot.context.embedded.tomcat; package org.springframework.boot.context.embedded.tomcat;
import org.apache.catalina.LifecycleException; import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.connector.Connector; import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.Tomcat;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -75,6 +76,10 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
}; };
awaitThread.setDaemon(false); awaitThread.setDaemon(false);
awaitThread.start(); awaitThread.start();
if (LifecycleState.FAILED.equals(this.tomcat.getConnector().getState())) {
this.tomcat.stop();
throw new IllegalStateException("Tomcat connector in failed state");
}
} }
catch (Exception ex) { catch (Exception ex) {
throw new EmbeddedServletContainerException( throw new EmbeddedServletContainerException(