Skip to content

Commit

Permalink
Allow shutdown of Javax WS Client Container though a SCI
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Jan 8, 2021
1 parent 3286f9a commit 774dac6
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 22 deletions.
5 changes: 5 additions & 0 deletions jetty-websocket/websocket-javax-client/pom.xml
Expand Up @@ -34,6 +34,11 @@
<artifactId>jetty-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-servlet-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-xml</artifactId>
Expand Down
Expand Up @@ -20,6 +20,7 @@
exports org.eclipse.jetty.websocket.javax.client;
exports org.eclipse.jetty.websocket.javax.client.internal to org.eclipse.jetty.websocket.javax.server;

requires static jetty.servlet.api;
requires org.slf4j;
requires org.eclipse.jetty.client;
requires org.eclipse.jetty.websocket.core.client;
Expand Down
@@ -0,0 +1,48 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.websocket.javax.client;

import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;

import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;

public class JavaxWebSocketClientShutdown extends ContainerLifeCycle implements ServletContainerInitializer, ServletContextListener
{
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException
{
JavaxWebSocketClientContainer.SHUTDOWN_CONTAINER.compareAndSet(null, this);
ctx.addListener(this);
}

@Override
public void contextInitialized(ServletContextEvent sce)
{
LifeCycle.start(this);
}

@Override
public void contextDestroyed(ServletContextEvent sce)
{
LifeCycle.stop(this);
removeBeans();
}
}
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import javax.websocket.ClientEndpoint;
import javax.websocket.ClientEndpointConfig;
Expand All @@ -34,6 +35,7 @@

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.ShutdownThread;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
Expand All @@ -58,6 +60,7 @@
public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer implements javax.websocket.WebSocketContainer
{
private static final Logger LOG = LoggerFactory.getLogger(JavaxWebSocketClientContainer.class);
public static final AtomicReference<ContainerLifeCycle> SHUTDOWN_CONTAINER = new AtomicReference<>();

protected WebSocketCoreClient coreClient;
protected Function<WebSocketComponents, WebSocketCoreClient> coreClientFactory;
Expand Down Expand Up @@ -287,6 +290,24 @@ protected void doStart() throws Exception
}

protected void doClientStart()
{
// If we are running in Jetty register shutdown with the ContextHandler.
// TODO: add test mode to disable this.
if (shutdownWithContextHandler(this))
return;

// If we are running inside a different ServletContainer we can register with the SHUTDOWN_CONTAINER static.
ContainerLifeCycle shutdownContainer = SHUTDOWN_CONTAINER.get();
if (shutdownContainer != null)
{
shutdownContainer.addManaged(this);
return;
}

ShutdownThread.register(this);
}

private boolean shutdownWithContextHandler(LifeCycle lifeCycle)
{
try
{
Expand All @@ -301,7 +322,7 @@ protected void doClientStart()

contextHandler.getClass()
.getMethod("addManaged", LifeCycle.class)
.invoke(contextHandler, this);
.invoke(contextHandler, lifeCycle);

AbstractLifeCycleListener shutdownListener = new AbstractLifeCycleListener()
{
Expand All @@ -324,22 +345,11 @@ public void lifeCycleStopping(LifeCycle event)
contextHandler.getClass()
.getMethod("addEventListener", EventListener.class)
.invoke(contextHandler, shutdownListener);
return true;
}
catch (Throwable ignored)
catch (Throwable throwable)
{
ShutdownThread.register(this);
return false;
}
}

@Override
protected void doStop() throws Exception
{
super.doStop();
doClientStop();
}

protected void doClientStop()
{
ShutdownThread.deregister(this);
}
}
@@ -0,0 +1 @@
org.eclipse.jetty.websocket.javax.client.JavaxWebSocketClientShutdown
Expand Up @@ -302,10 +302,4 @@ protected void doClientStart()
{
// Do nothing to avoid registration with the ShutdownThread.
}

@Override
protected void doClientStop()
{
// Do nothing to avoid de-registration with the ShutdownThread.
}
}
Expand Up @@ -30,6 +30,8 @@
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.listener.ContainerInitializer;
import org.eclipse.jetty.websocket.javax.client.JavaxWebSocketClientShutdown;
import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -63,13 +65,17 @@ public void before() throws Exception
{
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.addConnector(connector);

contextHandler = new ServletContextHandler();
contextHandler.setContextPath("/");
contextHandler.addServlet(new ServletHolder(new WebSocketClientInServlet()), "/");
server.setHandler(contextHandler);

// Because we are using embedded we must manually add the Javax WS Client Shutdown SCI.
// TODO: fix to not use ContainerInitializer.asContextListener
contextHandler.addEventListener(ContainerInitializer.asContextListener(new JavaxWebSocketClientShutdown()));

server.start();
serverUri = WSURI.toWebsocket(server.getURI());

Expand Down

0 comments on commit 774dac6

Please sign in to comment.