diff --git a/jetty-websocket/websocket-javax-client/src/main/java/module-info.java b/jetty-websocket/websocket-javax-client/src/main/java/module-info.java index 54323eb068b..c827261eb6f 100644 --- a/jetty-websocket/websocket-javax-client/src/main/java/module-info.java +++ b/jetty-websocket/websocket-javax-client/src/main/java/module-info.java @@ -20,6 +20,7 @@ module org.eclipse.jetty.websocket.javax.client exports org.eclipse.jetty.websocket.javax.client; exports org.eclipse.jetty.websocket.javax.client.internal to org.eclipse.jetty.websocket.javax.server; + requires org.slf4j; requires org.eclipse.jetty.client; requires org.eclipse.jetty.websocket.core.client; requires org.eclipse.jetty.websocket.javax.common; diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java index 6ac2a48d47c..c1974dceb55 100644 --- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java +++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java @@ -58,14 +58,7 @@ public class JavaxWebSocketClientContainerProvider extends ContainerProvider // TODO: do we want to provide a non-standard way to configure to always return the same clientContainer based on a config somewhere? (system.property?) JavaxWebSocketClientContainer clientContainer = new JavaxWebSocketClientContainer(); - try - { - clientContainer.start(); - } - catch (Exception e) - { - throw new RuntimeException("Unable to start Client Container", e); - } + LifeCycle.start(clientContainer); return clientContainer; } } diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientContainer.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientContainer.java index 28d22373794..e54f02df27a 100644 --- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientContainer.java +++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientContainer.java @@ -46,6 +46,8 @@ import org.eclipse.jetty.websocket.javax.common.JavaxWebSocketContainer; import org.eclipse.jetty.websocket.javax.common.JavaxWebSocketExtensionConfig; import org.eclipse.jetty.websocket.javax.common.JavaxWebSocketFrameHandler; import org.eclipse.jetty.websocket.javax.common.JavaxWebSocketFrameHandlerFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Container for Client use of the javax.websocket API. @@ -55,6 +57,8 @@ import org.eclipse.jetty.websocket.javax.common.JavaxWebSocketFrameHandlerFactor @ManagedObject("JSR356 Client Container") public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer implements javax.websocket.WebSocketContainer { + private static final Logger LOG = LoggerFactory.getLogger(JavaxWebSocketClientContainer.class); + protected WebSocketCoreClient coreClient; protected Function coreClientFactory; private final JavaxWebSocketClientFrameHandlerFactory frameHandlerFactory; @@ -295,12 +299,25 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple .getMethod("getContextHandler") .invoke(context); + contextHandler.getClass() + .getMethod("addManaged", LifeCycle.class) + .invoke(contextHandler, this); + AbstractLifeCycleListener shutdownListener = new AbstractLifeCycleListener() { @Override public void lifeCycleStopping(LifeCycle event) { - LifeCycle.stop(JavaxWebSocketClientContainer.this); + try + { + contextHandler.getClass() + .getMethod("removeBean", Object.class) + .invoke(contextHandler, JavaxWebSocketClientContainer.this); + } + catch (Throwable t) + { + LOG.warn("could not remove client WebSocketContainer bean from {}", contextHandler); + } } }; diff --git a/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/ClientInWebappTest.java b/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/ClientInWebappTest.java index d349b18dc65..14fb5a7099f 100644 --- a/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/ClientInWebappTest.java +++ b/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/ClientInWebappTest.java @@ -15,6 +15,7 @@ package org.eclipse.jetty.websocket.javax.tests; import java.io.IOException; import java.net.URI; +import java.util.Collection; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -43,11 +44,12 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; public class ClientInWebappTest { private Server server; + private ServletContextHandler contextHandler; private URI serverUri; private HttpClient httpClient; private volatile WebSocketContainer container; - public class ServerSocket extends HttpServlet + public class WebSocketClientInServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException @@ -64,9 +66,9 @@ public class ClientInWebappTest connector.setPort(8080); server.addConnector(connector); - ServletContextHandler contextHandler = new ServletContextHandler(); + contextHandler = new ServletContextHandler(); contextHandler.setContextPath("/"); - contextHandler.addServlet(new ServletHolder(new ServerSocket()), "/"); + contextHandler.addServlet(new ServletHolder(new WebSocketClientInServlet()), "/"); server.setHandler(contextHandler); server.start(); serverUri = WSURI.toWebsocket(server.getURI()); @@ -93,6 +95,11 @@ public class ClientInWebappTest JavaxWebSocketClientContainer clientContainer = (JavaxWebSocketClientContainer)container; assertThat(clientContainer.isRunning(), is(true)); + // The container should be a bean on the ContextHandler. + Collection containedBeans = contextHandler.getBeans(WebSocketContainer.class); + assertThat(containedBeans.size(), is(1)); + assertThat(containedBeans.toArray()[0], is(container)); + // The client should be attached to the servers LifeCycle and should stop with it. server.stop(); assertThat(clientContainer.isRunning(), is(false));