Detect Tomcat start failures
Detect if the tomcat container fails to start and propagate an exception to the caller.
This commit is contained in:
@@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -131,7 +131,13 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
|
|||||||
protected void onRefresh() {
|
protected void onRefresh() {
|
||||||
super.onRefresh();
|
super.onRefresh();
|
||||||
registerShutdownHook();
|
registerShutdownHook();
|
||||||
createEmbeddedServletContainer();
|
try {
|
||||||
|
createEmbeddedServletContainer();
|
||||||
|
}
|
||||||
|
catch (Throwable ex) {
|
||||||
|
throw new ApplicationContextException("Unable to start embedded container",
|
||||||
|
ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -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(
|
||||||
|
|||||||
Reference in New Issue
Block a user