Skip to content

Commit 81a4548

Browse files
author
Dave Syer
committed
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 spring-projectsgh-1232
1 parent ede385d commit 81a4548

File tree

8 files changed

+28
-1
lines changed

8 files changed

+28
-1
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainer.java

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ private synchronized void initialize() {
7575
}
7676
}
7777
catch (Exception ex) {
78+
try {
79+
// Ensure process isn't left running
80+
this.server.stop();
81+
}
82+
catch (Exception e) {
83+
}
7884
throw new EmbeddedServletContainerException(
7985
"Unable to start embedded Jetty servlet container", ex);
8086
}

spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ private synchronized void initialize() throws EmbeddedServletContainerException
9595
this.tomcat.stop();
9696
throw new IllegalStateException("Tomcat connector in failed state");
9797
}
98+
9899
}
99100
catch (Exception ex) {
100101
throw new EmbeddedServletContainerException(
@@ -151,6 +152,15 @@ public void start() throws EmbeddedServletContainerException {
151152
if (connector != null && this.autoStart) {
152153
startConnector(connector);
153154
}
155+
// Ensure process isn't left running if it actually failed to start
156+
if (LifecycleState.FAILED.equals(this.tomcat.getConnector().getState())) {
157+
try {
158+
this.tomcat.stop();
159+
}
160+
catch (LifecycleException e) {
161+
}
162+
throw new IllegalStateException("Tomcat connector in failed state");
163+
}
154164
}
155165

156166
private void addPreviouslyRemovedConnectors() {
@@ -213,11 +223,11 @@ public synchronized void stop() throws EmbeddedServletContainerException {
213223
try {
214224
try {
215225
this.tomcat.stop();
226+
this.tomcat.destroy();
216227
}
217228
catch (LifecycleException ex) {
218229
// swallow and continue
219230
}
220-
this.tomcat.destroy();
221231
}
222232
catch (Exception ex) {
223233
throw new EmbeddedServletContainerException("Unable to stop embedded Tomcat",

spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class LoggingApplicationListener implements SmartApplicationListener {
8888
LOG_LEVEL_LOGGERS.add(LogLevel.DEBUG, "org.springframework.boot");
8989
LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.springframework");
9090
LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.apache.tomcat");
91+
LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.apache.catalina");
9192
LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.eclipse.jetty");
9293
LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.hibernate.tool.hbm2ddl");
9394
}

spring-boot/src/main/resources/org/springframework/boot/logging/java/basic-logging.properties

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ org.apache.coyote.http11.Http11NioProtocol.level = WARNING
99
org.crsh.plugin.level = WARNING
1010
org.apache.tomcat.util.net.NioSelectorPool.level = WARNING
1111
org.apache.catalina.startup.DigesterFactory.level = SEVERE
12+
org.apache.catalina.util.LifecycleBase.level = SEVERE
13+
org.eclipse.jetty.util.component.AbstractLifeCycle.level = SEVERE

spring-boot/src/main/resources/org/springframework/boot/logging/java/logging.properties

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ org.apache.coyote.http11.Http11NioProtocol.level = WARNING
1616
org.crsh.plugin.level = WARNING
1717
org.apache.tomcat.util.net.NioSelectorPool.level = WARNING
1818
org.apache.catalina.startup.DigesterFactory.level = SEVERE
19+
org.apache.catalina.util.LifecycleBase.level = SEVERE
20+
org.eclipse.jetty.util.component.AbstractLifeCycle.level = SEVERE

spring-boot/src/main/resources/org/springframework/boot/logging/log4j/basic-log4j.properties

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ log4j.category.org.apache.coyote.http11.Http11NioProtocol=WARN
1313
log4j.category.org.crsh.plugin=WARN
1414
log4j.category.org.apache.tomcat.util.net.NioSelectorPool=WARN
1515
log4j.category.org.apache.catalina.startup.DigesterFactory=ERROR
16+
log4j.category.org.apache.catalina.util.LifecycleBase=ERROR
17+
log4j.category.org.eclipse.jetty.util.component.AbstractLifeCycle=ERROR

spring-boot/src/main/resources/org/springframework/boot/logging/log4j/log4j.properties

+2
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ log4j.category.org.apache.coyote.http11.Http11NioProtocol=WARN
2121
log4j.category.org.crsh.plugin=WARN
2222
log4j.category.org.apache.tomcat.util.net.NioSelectorPool=WARN
2323
log4j.category.org.apache.catalina.startup.DigesterFactory=ERROR
24+
log4j.category.org.apache.catalina.util.LifecycleBase=ERROR
25+
log4j.category.org.eclipse.jetty.util.component.AbstractLifeCycle=ERROR

spring-boot/src/main/resources/org/springframework/boot/logging/logback/basic.xml

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
<logger name="org.crsh.plugin" level="WARN"/>
2525
<logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"/>
2626
<logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR"/>
27+
<logger name="org.apache.catalina.util.LifecycleBase" level="ERROR"/>
28+
<logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="ERROR"/>
2729
<logger name="org.thymeleaf" additivity="false">
2830
<appender-ref ref="DEBUG_LEVEL_REMAPPER"/>
2931
</logger>

0 commit comments

Comments
 (0)