Skip to content

Commit 339fd74

Browse files
committed
Call ServletContextListener.contextDestroyed() when Undertow is stopped
Previously, when the embedded Undertow container was stopped, the servlet deployment was stopped but it was not undeployed. This meant that contextDestroyed() callback of any registered ServletContextListeners was not called. This commit updates UndertowEmbeddedServletContainer to call undeploy on the deployment manager in addition to the existing call to stop. Undeploying the servlet deployment calls Undertow to drive the contextDestroyed callback on any registered ServletContextListeners. Closes gh-13134
1 parent f07daf8 commit 339fd74

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ public void stop() throws EmbeddedServletContainerException {
333333
this.started = false;
334334
try {
335335
this.manager.stop();
336+
this.manager.undeploy();
336337
this.undertow.stop();
337338
}
338339
catch (Exception ex) {

spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
import javax.net.ssl.SSLException;
5656
import javax.servlet.GenericServlet;
5757
import javax.servlet.ServletContext;
58+
import javax.servlet.ServletContextEvent;
59+
import javax.servlet.ServletContextListener;
5860
import javax.servlet.ServletException;
5961
import javax.servlet.ServletRequest;
6062
import javax.servlet.ServletResponse;
@@ -110,6 +112,7 @@
110112
import static org.hamcrest.CoreMatchers.notNullValue;
111113
import static org.junit.Assert.fail;
112114
import static org.mockito.BDDMockito.given;
115+
import static org.mockito.Matchers.any;
113116
import static org.mockito.Matchers.anyObject;
114117
import static org.mockito.Mockito.inOrder;
115118
import static org.mockito.Mockito.mock;
@@ -1035,6 +1038,26 @@ public void explodedWarFileDocumentRootWhenRunningFromPackagedWar() throws Excep
10351038
assertThat(documentRoot).isNull();
10361039
}
10371040

1041+
@Test
1042+
public void servletContextListenerContextDestroyedIsCalledWhenContainerIsStopped()
1043+
throws Exception {
1044+
final ServletContextListener listener = mock(ServletContextListener.class);
1045+
AbstractEmbeddedServletContainerFactory factory = getFactory();
1046+
this.container = factory
1047+
.getEmbeddedServletContainer(new ServletContextInitializer() {
1048+
1049+
@Override
1050+
public void onStartup(ServletContext servletContext)
1051+
throws ServletException {
1052+
servletContext.addListener(listener);
1053+
}
1054+
1055+
});
1056+
this.container.start();
1057+
this.container.stop();
1058+
verify(listener).contextDestroyed(any(ServletContextEvent.class));
1059+
}
1060+
10381061
protected abstract void addConnector(int port,
10391062
AbstractEmbeddedServletContainerFactory factory);
10401063

0 commit comments

Comments
 (0)