From bd577f15152f5a25271375ae7eb3d7097fb3a9ee Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 28 Jul 2014 14:38:11 +0100 Subject: [PATCH] Rework Jetty startup so connectors are only started once Previously the server was started to make the ServletContext available, then, to prevent requests from being handled before the application context had been started, the connectors were stopped. Once application context startup had completed, the connectors were then started again. In addition to being somewhat inefficient, this caused problems on FreeBSD where stopping the connector didn't free up the port quickly enough for the subsequent start to then be able to bind to it. This commit updates the Jetty startup logic to be closer to the logic that's used for Tomcat. Before the server is started, the configured connectors are cached and then removed. The server is then started without any connectors. Once application context startup has completed, the connectors are reinstated and started. Fixes #968 --- .../jetty/JettyEmbeddedServletContainer.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainer.java index a4c0039d482..2e4df38e457 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainer.java @@ -44,6 +44,8 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer { private final boolean autoStart; + private Connector[] connectors; + /** * Create a new {@link JettyEmbeddedServletContainer} instance. * @param server the underlying Jetty server @@ -65,14 +67,13 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer { private synchronized void initialize() { try { + // Cache and clear the connectors to prevent requests being handled before + // the application context is ready + this.connectors = this.server.getConnectors(); + this.server.setConnectors(null); + + // Start the server so that the ServletContext is available this.server.start(); - // Start the server so the ServletContext is available, but stop the - // connectors to prevent requests from being handled before the Spring context - // is ready: - Connector[] connectors = this.server.getConnectors(); - for (Connector connector : connectors) { - connector.stop(); - } } catch (Exception ex) { try { @@ -88,6 +89,8 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer { @Override public void start() throws EmbeddedServletContainerException { + this.server.setConnectors(this.connectors); + if (!this.autoStart) { return; }