diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java index 8f4149ddd03..67c75d0838f 100644 --- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java +++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java @@ -77,6 +77,9 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont private final int id = ThreadLocalRandom.current().nextInt(); + // defaults to true for backwards compatibility + private boolean stopAtShutdown = true; + /** * Instantiate a WebSocketClient with defaults */ @@ -552,7 +555,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont private synchronized void init() throws IOException { - if (!ShutdownThread.isRegistered(this)) + if (isStopAtShutdown() && !ShutdownThread.isRegistered(this)) { ShutdownThread.register(this); } @@ -694,6 +697,31 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont return this.httpClient; } + /** + * Set JVM shutdown behavior. + * @param stop If true, this client instance will be explicitly stopped when the + * JVM is shutdown. Otherwise the application is responsible for maintaining the WebSocketClient lifecycle. + * @see Runtime#addShutdownHook(Thread) + * @see ShutdownThread + */ + public synchronized void setStopAtShutdown(boolean stop) + { + if (stop) + { + if (!stopAtShutdown && isStarted() && !ShutdownThread.isRegistered(this)) + ShutdownThread.register(this); + } + else + ShutdownThread.deregister(this); + + stopAtShutdown = stop; + } + + public boolean isStopAtShutdown() + { + return stopAtShutdown; + } + @Override public String toString() {