WebSocket server now only closes gracefully if the Server stopTimeout is set

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-07-20 15:49:15 +10:00
parent 0818f54be8
commit 9f7f2e3e56
5 changed files with 37 additions and 8 deletions

View File

@ -27,7 +27,6 @@ import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContext;
@ -469,14 +468,12 @@ public class Server extends HandlerWrapper implements Attributes
MultiException mex = new MultiException();
// Initiate graceful shutdown but only wait for it if stopTimeout is set.
CompletableFuture<Void> shutdown = Graceful.shutdown(this);
if (getStopTimeout() > 0)
{
long end = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(getStopTimeout());
try
{
shutdown.get(getStopTimeout(), TimeUnit.MILLISECONDS);
Graceful.shutdown(this).get(getStopTimeout(), TimeUnit.MILLISECONDS);
}
catch (Throwable e)
{

View File

@ -307,9 +307,22 @@ public class JavaxWebSocketServerContainer extends JavaxWebSocketClientContainer
@Override
public CompletableFuture<Void> shutdown()
{
LifeCycle.stop(sessionTracker);
CompletableFuture<Void> shutdown = new CompletableFuture<>();
shutdown.complete(null);
new Thread(() ->
{
try
{
LifeCycle.stop(sessionTracker);
}
catch (Throwable t)
{
LOG.warn("Error while stopping SessionTracker", t);
}
finally
{
shutdown.complete(null);
}
}).start();
return shutdown;
}

View File

@ -63,6 +63,9 @@ public class GracefulCloseTest
server.start();
serverUri = WSURI.toWebsocket(server.getURI());
// StopTimeout is necessary for the websocket server sessions to gracefully close.
server.setStopTimeout(1000);
client = new JavaxWebSocketClientContainer();
client.start();
}

View File

@ -267,9 +267,22 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements
@Override
public CompletableFuture<Void> shutdown()
{
LifeCycle.stop(sessionTracker);
CompletableFuture<Void> shutdown = new CompletableFuture<>();
shutdown.complete(null);
new Thread(() ->
{
try
{
LifeCycle.stop(sessionTracker);
}
catch (Throwable t)
{
LOG.warn("Error while stopping SessionTracker", t);
}
finally
{
shutdown.complete(null);
}
}).start();
return shutdown;
}

View File

@ -59,6 +59,9 @@ public class GracefulCloseTest
server.start();
serverUri = WSURI.toWebsocket(server.getURI());
// StopTimeout is necessary for the websocket server sessions to gracefully close.
server.setStopTimeout(1000);
client = new WebSocketClient();
client.start();
}