Skip to content

Commit 826b18f

Browse files
committed
Merge pull request #16892 from jpmsilva
* pr/16892: Polish "Allow Tomcat be destroyed regardless of exceptions" Allow Tomcat be destroyed regardless of exceptions
2 parents c519932 + 0937362 commit 826b18f

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -122,6 +122,7 @@ private void initialize() throws WebServerException {
122122
}
123123
catch (Exception ex) {
124124
stopSilently();
125+
destroySilently();
125126
throw new WebServerException("Unable to start embedded Tomcat", ex);
126127
}
127128
}
@@ -242,6 +243,15 @@ private void stopSilently() {
242243
}
243244
}
244245

246+
private void destroySilently() {
247+
try {
248+
this.tomcat.destroy();
249+
}
250+
catch (LifecycleException ex) {
251+
// Ignore
252+
}
253+
}
254+
245255
private void stopTomcat() throws LifecycleException {
246256
if (Thread.currentThread()
247257
.getContextClassLoader() instanceof TomcatEmbeddedWebappClassLoader) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,27 @@ protected void doPost(HttpServletRequest req,
523523
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
524524
}
525525

526+
@Test
527+
public void exceptionThrownOnContextListenerDestroysServer() {
528+
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0) {
529+
530+
@Override
531+
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
532+
try {
533+
return super.getTomcatWebServer(tomcat);
534+
}
535+
finally {
536+
assertThat(tomcat.getServer().getState())
537+
.isEqualTo(LifecycleState.DESTROYED);
538+
}
539+
}
540+
541+
};
542+
assertThatExceptionOfType(WebServerException.class)
543+
.isThrownBy(() -> factory.getWebServer((context) -> context
544+
.addListener(new FailingServletContextListener())));
545+
}
546+
526547
@Override
527548
protected JspServlet getJspServlet() throws ServletException {
528549
Tomcat tomcat = ((TomcatWebServer) this.webServer).getTomcat();

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,15 @@ public void init() throws ServletException {
14011401

14021402
}
14031403

1404+
public static class FailingServletContextListener implements ServletContextListener {
1405+
1406+
@Override
1407+
public void contextInitialized(ServletContextEvent sce) {
1408+
throw new FailingServletException();
1409+
}
1410+
1411+
}
1412+
14041413
private static class FailingServletException extends RuntimeException {
14051414

14061415
FailingServletException() {

0 commit comments

Comments
 (0)