From 339fd74810549d7ba9e864b83a443620ec570ff3 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 14 May 2018 19:48:45 +0100 Subject: [PATCH] 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 --- .../UndertowEmbeddedServletContainer.java | 1 + ...tEmbeddedServletContainerFactoryTests.java | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java index c0e9a81ff2..262ffdd660 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java @@ -333,6 +333,7 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine this.started = false; try { this.manager.stop(); + this.manager.undeploy(); this.undertow.stop(); } catch (Exception ex) { diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java index 59a9317733..86f9f54165 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java @@ -55,6 +55,8 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; import javax.servlet.GenericServlet; import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; @@ -110,6 +112,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.fail; import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; @@ -1035,6 +1038,26 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { assertThat(documentRoot).isNull(); } + @Test + public void servletContextListenerContextDestroyedIsCalledWhenContainerIsStopped() + throws Exception { + final ServletContextListener listener = mock(ServletContextListener.class); + AbstractEmbeddedServletContainerFactory factory = getFactory(); + this.container = factory + .getEmbeddedServletContainer(new ServletContextInitializer() { + + @Override + public void onStartup(ServletContext servletContext) + throws ServletException { + servletContext.addListener(listener); + } + + }); + this.container.start(); + this.container.stop(); + verify(listener).contextDestroyed(any(ServletContextEvent.class)); + } + protected abstract void addConnector(int port, AbstractEmbeddedServletContainerFactory factory);