From a411c94b0db274a5950846941477d1873e193578 Mon Sep 17 00:00:00 2001 From: Andrei Pangin Date: Sun, 27 Nov 2022 15:24:26 +0000 Subject: [PATCH] Gracefully close sessions when stopping Server --- src/one/nio/server/Server.java | 9 +++++++++ src/one/nio/server/ServerConfig.java | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/one/nio/server/Server.java b/src/one/nio/server/Server.java index fa3f425..f5256aa 100755 --- a/src/one/nio/server/Server.java +++ b/src/one/nio/server/Server.java @@ -48,6 +48,7 @@ public class Server implements ServerMXBean { protected boolean useWorkers; protected final WorkerPool workers; protected final CleanupThread cleanup; + protected boolean closeSessions; public Server(ServerConfig config) throws IOException { List acceptors = new ArrayList<>(); @@ -79,6 +80,8 @@ public Server(ServerConfig config) throws IOException { this.cleanup = new CleanupThread(selectors, config.keepAlive); + this.closeSessions = config.closeSessions; + this.selectorStats = new SelectorStats(); this.queueStats = new QueueStats(); } @@ -142,6 +145,7 @@ public synchronized void reconfigure(ServerConfig config) throws IOException { } cleanup.update(this.selectors, config.keepAlive); + closeSessions = config.closeSessions; } public synchronized void start() { @@ -175,6 +179,11 @@ public synchronized void stop() { } for (SelectorThread selector : selectors) { + if (closeSessions) { + for (Session session : selector.selector) { + session.close(); + } + } selector.shutdown(); } diff --git a/src/one/nio/server/ServerConfig.java b/src/one/nio/server/ServerConfig.java index fc19955..3fb2334 100644 --- a/src/one/nio/server/ServerConfig.java +++ b/src/one/nio/server/ServerConfig.java @@ -35,6 +35,7 @@ public class ServerConfig { public int keepAlive; public int threadPriority = Thread.NORM_PRIORITY; public SchedulingPolicy schedulingPolicy; + public boolean closeSessions; public ServerConfig() { } @@ -58,6 +59,7 @@ private ServerConfig(ConnectionString conn) { this.queueTime = conn.getIntParam("queueTime", 0) / 1000; this.threadPriority = conn.getIntParam("threadPriority", Thread.NORM_PRIORITY); this.schedulingPolicy = SchedulingPolicy.valueOf(conn.getStringParam("schedulingPolicy", "OTHER")); + this.closeSessions = conn.getBooleanParam("closeSessions", false); } // Do not use for new servers! Use ConfigParser instead