From 81a4548561dc63f0a0d91f149e3b37b90e1053d4 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 10 Jul 2014 15:14:25 +0100 Subject: [PATCH] Ensure embedded containers actually stop if there is an error on startup Both embedded containers need to be checked after starting to ensure that they are actually running. With Jetty it's just a question of catching an exception but with Tomcat it's harder (the current solution involves duplicating some code from initialize() into start() essentially checking the lifecycle state). Also adjusted the log levels to prevent noise at WARN level by default when this happens (since the exception is logged and rethrown anyway). There is still the issue of whether to fail the build in Maven or Gradle (separate issue really). Fixes gh-1232 --- .../jetty/JettyEmbeddedServletContainer.java | 6 ++++++ .../tomcat/TomcatEmbeddedServletContainer.java | 12 +++++++++++- .../boot/logging/LoggingApplicationListener.java | 1 + .../boot/logging/java/basic-logging.properties | 2 ++ .../boot/logging/java/logging.properties | 2 ++ .../boot/logging/log4j/basic-log4j.properties | 2 ++ .../boot/logging/log4j/log4j.properties | 2 ++ .../springframework/boot/logging/logback/basic.xml | 2 ++ 8 files changed, 28 insertions(+), 1 deletion(-) 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 4dfb0a7acc..a4c0039d48 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 @@ -75,6 +75,12 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer { } } catch (Exception ex) { + try { + // Ensure process isn't left running + this.server.stop(); + } + catch (Exception e) { + } throw new EmbeddedServletContainerException( "Unable to start embedded Jetty servlet container", ex); } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java index 59e96286f2..4a9a93ebf5 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java @@ -95,6 +95,7 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer this.tomcat.stop(); throw new IllegalStateException("Tomcat connector in failed state"); } + } catch (Exception ex) { throw new EmbeddedServletContainerException( @@ -151,6 +152,15 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer if (connector != null && this.autoStart) { startConnector(connector); } + // Ensure process isn't left running if it actually failed to start + if (LifecycleState.FAILED.equals(this.tomcat.getConnector().getState())) { + try { + this.tomcat.stop(); + } + catch (LifecycleException e) { + } + throw new IllegalStateException("Tomcat connector in failed state"); + } } private void addPreviouslyRemovedConnectors() { @@ -213,11 +223,11 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer try { try { this.tomcat.stop(); + this.tomcat.destroy(); } catch (LifecycleException ex) { // swallow and continue } - this.tomcat.destroy(); } catch (Exception ex) { throw new EmbeddedServletContainerException("Unable to stop embedded Tomcat", diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java index 8d58806fc1..8f6ad83167 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java @@ -88,6 +88,7 @@ public class LoggingApplicationListener implements SmartApplicationListener { LOG_LEVEL_LOGGERS.add(LogLevel.DEBUG, "org.springframework.boot"); LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.springframework"); LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.apache.tomcat"); + LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.apache.catalina"); LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.eclipse.jetty"); LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.hibernate.tool.hbm2ddl"); } diff --git a/spring-boot/src/main/resources/org/springframework/boot/logging/java/basic-logging.properties b/spring-boot/src/main/resources/org/springframework/boot/logging/java/basic-logging.properties index ae39617ab9..73c81a46ed 100644 --- a/spring-boot/src/main/resources/org/springframework/boot/logging/java/basic-logging.properties +++ b/spring-boot/src/main/resources/org/springframework/boot/logging/java/basic-logging.properties @@ -9,3 +9,5 @@ org.apache.coyote.http11.Http11NioProtocol.level = WARNING org.crsh.plugin.level = WARNING org.apache.tomcat.util.net.NioSelectorPool.level = WARNING org.apache.catalina.startup.DigesterFactory.level = SEVERE +org.apache.catalina.util.LifecycleBase.level = SEVERE +org.eclipse.jetty.util.component.AbstractLifeCycle.level = SEVERE diff --git a/spring-boot/src/main/resources/org/springframework/boot/logging/java/logging.properties b/spring-boot/src/main/resources/org/springframework/boot/logging/java/logging.properties index 2c83dcfb5b..a37a6fa096 100644 --- a/spring-boot/src/main/resources/org/springframework/boot/logging/java/logging.properties +++ b/spring-boot/src/main/resources/org/springframework/boot/logging/java/logging.properties @@ -16,3 +16,5 @@ org.apache.coyote.http11.Http11NioProtocol.level = WARNING org.crsh.plugin.level = WARNING org.apache.tomcat.util.net.NioSelectorPool.level = WARNING org.apache.catalina.startup.DigesterFactory.level = SEVERE +org.apache.catalina.util.LifecycleBase.level = SEVERE +org.eclipse.jetty.util.component.AbstractLifeCycle.level = SEVERE diff --git a/spring-boot/src/main/resources/org/springframework/boot/logging/log4j/basic-log4j.properties b/spring-boot/src/main/resources/org/springframework/boot/logging/log4j/basic-log4j.properties index 253900fe6b..28b637638f 100644 --- a/spring-boot/src/main/resources/org/springframework/boot/logging/log4j/basic-log4j.properties +++ b/spring-boot/src/main/resources/org/springframework/boot/logging/log4j/basic-log4j.properties @@ -13,3 +13,5 @@ log4j.category.org.apache.coyote.http11.Http11NioProtocol=WARN log4j.category.org.crsh.plugin=WARN log4j.category.org.apache.tomcat.util.net.NioSelectorPool=WARN log4j.category.org.apache.catalina.startup.DigesterFactory=ERROR +log4j.category.org.apache.catalina.util.LifecycleBase=ERROR +log4j.category.org.eclipse.jetty.util.component.AbstractLifeCycle=ERROR diff --git a/spring-boot/src/main/resources/org/springframework/boot/logging/log4j/log4j.properties b/spring-boot/src/main/resources/org/springframework/boot/logging/log4j/log4j.properties index 0cb4585593..58baefaced 100644 --- a/spring-boot/src/main/resources/org/springframework/boot/logging/log4j/log4j.properties +++ b/spring-boot/src/main/resources/org/springframework/boot/logging/log4j/log4j.properties @@ -21,3 +21,5 @@ log4j.category.org.apache.coyote.http11.Http11NioProtocol=WARN log4j.category.org.crsh.plugin=WARN log4j.category.org.apache.tomcat.util.net.NioSelectorPool=WARN log4j.category.org.apache.catalina.startup.DigesterFactory=ERROR +log4j.category.org.apache.catalina.util.LifecycleBase=ERROR +log4j.category.org.eclipse.jetty.util.component.AbstractLifeCycle=ERROR diff --git a/spring-boot/src/main/resources/org/springframework/boot/logging/logback/basic.xml b/spring-boot/src/main/resources/org/springframework/boot/logging/logback/basic.xml index 5be82ccb4c..cbddd773bf 100644 --- a/spring-boot/src/main/resources/org/springframework/boot/logging/logback/basic.xml +++ b/spring-boot/src/main/resources/org/springframework/boot/logging/logback/basic.xml @@ -24,6 +24,8 @@ + +