From b680d1deea7ea45a57a9cdb1b05ed52c0c4ff6a7 Mon Sep 17 00:00:00 2001 From: Lucas Fairchild-Madar Date: Fri, 25 Aug 2017 11:23:08 -0700 Subject: [PATCH 1/4] Allow for configuring WebSocketClient JVM lifecycle Signed-off-by: Lucas Fairchild-Madar --- .../websocket/client/WebSocketClient.java | 59 +++++++++++++++---- 1 file changed, 48 insertions(+), 11 deletions(-) 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..17eff77fbba 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 */ @@ -378,21 +381,37 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont if (LOG.isDebugEnabled()) LOG.debug("connect websocket {} to {}",websocket,toUri); - init(); - WebSocketUpgradeRequest wsReq = new WebSocketUpgradeRequest(this,httpClient,request); wsReq.setUpgradeListener(upgradeListener); return wsReq.sendAsync(); } + @Override + protected void doStart() throws Exception + { + if (LOG.isDebugEnabled()) + LOG.debug("Starting {}",this); + + if (isStopAtShutdown()) { + ShutdownThread.register(this); + } + + super.doStart(); + + if (LOG.isDebugEnabled()) + LOG.debug("Started {}",this); + } + @Override protected void doStop() throws Exception { if (LOG.isDebugEnabled()) LOG.debug("Stopping {}",this); - ShutdownThread.deregister(this); + if (isStopAtShutdown()) { + ShutdownThread.deregister(this); + } super.doStop(); @@ -550,14 +569,6 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont return httpClient.getSslContextFactory(); } - private synchronized void init() throws IOException - { - if (!ShutdownThread.isRegistered(this)) - { - ShutdownThread.register(this); - } - } - /** * Factory method for new ConnectionManager * @@ -694,6 +705,32 @@ 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 void setStopAtShutdown(boolean stop) + { + if (stop) + { + if (!stopAtShutdown && isStarted()) { + ShutdownThread.register(this); + } + } + else { + ShutdownThread.deregister(this); + } + + stopAtShutdown = stop; + } + + public boolean isStopAtShutdown() { + return stopAtShutdown; + } + @Override public String toString() { From 7d1d7a9724640ed121799d9f811908446d8bb0f9 Mon Sep 17 00:00:00 2001 From: Lucas Fairchild-Madar Date: Fri, 25 Aug 2017 12:33:41 -0700 Subject: [PATCH 2/4] Preserve delayed initialization semantics Signed-off-by: Lucas Fairchild-Madar --- .../websocket/client/WebSocketClient.java | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) 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 17eff77fbba..ec7a769cdfa 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 @@ -381,37 +381,21 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont if (LOG.isDebugEnabled()) LOG.debug("connect websocket {} to {}",websocket,toUri); + init(); + WebSocketUpgradeRequest wsReq = new WebSocketUpgradeRequest(this,httpClient,request); wsReq.setUpgradeListener(upgradeListener); return wsReq.sendAsync(); } - @Override - protected void doStart() throws Exception - { - if (LOG.isDebugEnabled()) - LOG.debug("Starting {}",this); - - if (isStopAtShutdown()) { - ShutdownThread.register(this); - } - - super.doStart(); - - if (LOG.isDebugEnabled()) - LOG.debug("Started {}",this); - } - @Override protected void doStop() throws Exception { if (LOG.isDebugEnabled()) LOG.debug("Stopping {}",this); - if (isStopAtShutdown()) { - ShutdownThread.deregister(this); - } + ShutdownThread.deregister(this); super.doStop(); @@ -569,6 +553,14 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont return httpClient.getSslContextFactory(); } + private synchronized void init() throws IOException + { + if (isStopAtShutdown() && !ShutdownThread.isRegistered(this)) + { + ShutdownThread.register(this); + } + } + /** * Factory method for new ConnectionManager * @@ -716,7 +708,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont { if (stop) { - if (!stopAtShutdown && isStarted()) { + if (!stopAtShutdown && isStarted() && !ShutdownThread.isRegistered(this)) { ShutdownThread.register(this); } } From 84ff1e3a66df6fd6040d5947dcea8be407eec9f8 Mon Sep 17 00:00:00 2001 From: Lucas Fairchild-Madar Date: Fri, 25 Aug 2017 12:35:45 -0700 Subject: [PATCH 3/4] Matching coding style; block potential race condition Signed-off-by: Lucas Fairchild-Madar --- .../eclipse/jetty/websocket/client/WebSocketClient.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 ec7a769cdfa..ac5b9377e05 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 @@ -704,15 +704,17 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont * @see Runtime#addShutdownHook(Thread) * @see ShutdownThread */ - public void setStopAtShutdown(boolean stop) + public synchronized void setStopAtShutdown(boolean stop) { if (stop) { - if (!stopAtShutdown && isStarted() && !ShutdownThread.isRegistered(this)) { + if (!stopAtShutdown && isStarted() && !ShutdownThread.isRegistered(this)) + { ShutdownThread.register(this); } } - else { + else + { ShutdownThread.deregister(this); } From b24c4e1a8a2b6ca95a057f5a569b6dd675d013dd Mon Sep 17 00:00:00 2001 From: Lucas Fairchild-Madar Date: Fri, 25 Aug 2017 12:36:48 -0700 Subject: [PATCH 4/4] further match coding style --- .../eclipse/jetty/websocket/client/WebSocketClient.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) 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 ac5b9377e05..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 @@ -709,19 +709,16 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont if (stop) { if (!stopAtShutdown && isStarted() && !ShutdownThread.isRegistered(this)) - { ShutdownThread.register(this); - } } else - { ShutdownThread.deregister(this); - } stopAtShutdown = stop; } - public boolean isStopAtShutdown() { + public boolean isStopAtShutdown() + { return stopAtShutdown; }