Make Jetty Server fail when its WebAppContext fails to start

Closes gh-13803
This commit is contained in:
Andy Wilkinson 2018-07-18 10:55:39 +01:00
parent 912eb53d7a
commit 56cb968b62
3 changed files with 41 additions and 1 deletions

View File

@ -104,7 +104,7 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer {
this.server.start();
this.server.setStopAtShutdown(false);
}
catch (Exception ex) {
catch (Throwable ex) {
// Ensure process isn't left running
stopSilently();
throw new EmbeddedServletContainerException(

View File

@ -368,6 +368,7 @@ public class JettyEmbeddedServletContainerFactory
Configuration[] configurations = getWebAppContextConfigurations(context,
initializersToUse);
context.setConfigurations(configurations);
context.setThrowUnavailableOnStartupException(true);
configureSession(context);
postProcessWebAppContext(context);
}

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import java.net.InetAddress;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@ -28,6 +29,8 @@ import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
@ -369,6 +372,42 @@ public class JettyEmbeddedServletContainerFactoryTests
}
}
@Test
public void faultyListenerCausesStartFailure() throws Exception {
JettyEmbeddedServletContainerFactory factory = getFactory();
factory.addServerCustomizers(new JettyServerCustomizer() {
@Override
public void customize(Server server) {
Collection<WebAppContext> contexts = server.getBeans(WebAppContext.class);
contexts.iterator().next().addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent sce) {
throw new RuntimeException();
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
});
}
});
this.thrown.expect(EmbeddedServletContainerException.class);
JettyEmbeddedServletContainer jettyContainer = (JettyEmbeddedServletContainer) factory
.getEmbeddedServletContainer();
try {
jettyContainer.start();
}
finally {
QueuedThreadPool threadPool = (QueuedThreadPool) jettyContainer.getServer()
.getThreadPool();
assertThat(threadPool.isRunning()).isFalse();
}
}
@Test
public void startFailsWhenThreadPoolIsTooSmall() throws Exception {
JettyEmbeddedServletContainerFactory factory = getFactory();