Issue #1777 - configuration for jetty-10 WebSocketClient to be stopped at shutdown

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan 2019-11-07 11:02:25 +11:00 committed by GitHub
parent f55fbdb7eb
commit 2310196532
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 13 deletions

View File

@ -41,6 +41,7 @@ import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.ShutdownThread;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
@ -65,7 +66,8 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketPoli
private final List<WebSocketSessionListener> sessionListeners = new CopyOnWriteArrayList<>();
private final SessionTracker sessionTracker = new SessionTracker();
private final FrameHandler.ConfigurationCustomizer configurationCustomizer = new FrameHandler.ConfigurationCustomizer();
private WebSocketComponents components = new WebSocketComponents();
private final WebSocketComponents components = new WebSocketComponents();
private boolean stopAtShutdown = false;
/**
* Instantiate a WebSocketClient with defaults
@ -340,6 +342,31 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketPoli
return getHttpClient().getSslContextFactory();
}
/**
* 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 && !ShutdownThread.isRegistered(this))
ShutdownThread.register(this);
}
else
ShutdownThread.deregister(this);
stopAtShutdown = stop;
}
public boolean isStopAtShutdown()
{
return stopAtShutdown;
}
@Override
public String toString()
{

View File

@ -28,7 +28,6 @@ import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.ShutdownThread;
import org.eclipse.jetty.websocket.core.ExtensionConfig;
import org.eclipse.jetty.websocket.core.FrameHandler;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
@ -92,20 +91,9 @@ public class WebSocketCoreClient extends ContainerLifeCycle
if (LOG.isDebugEnabled())
LOG.debug("connect to websocket {}", request.getURI());
init();
return request.sendAsync();
}
// TODO: review need for this.
private synchronized void init() throws IOException
{
if (!ShutdownThread.isRegistered(this))
{
ShutdownThread.register(this);
}
}
public WebSocketExtensionRegistry getExtensionRegistry()
{
return components.getExtensionRegistry();