Skip to content

Commit a411c94

Browse files
committed
Gracefully close sessions when stopping Server
1 parent 48c1843 commit a411c94

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/one/nio/server/Server.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class Server implements ServerMXBean {
4848
protected boolean useWorkers;
4949
protected final WorkerPool workers;
5050
protected final CleanupThread cleanup;
51+
protected boolean closeSessions;
5152

5253
public Server(ServerConfig config) throws IOException {
5354
List<AcceptorThread> acceptors = new ArrayList<>();
@@ -79,6 +80,8 @@ public Server(ServerConfig config) throws IOException {
7980

8081
this.cleanup = new CleanupThread(selectors, config.keepAlive);
8182

83+
this.closeSessions = config.closeSessions;
84+
8285
this.selectorStats = new SelectorStats();
8386
this.queueStats = new QueueStats();
8487
}
@@ -142,6 +145,7 @@ public synchronized void reconfigure(ServerConfig config) throws IOException {
142145
}
143146

144147
cleanup.update(this.selectors, config.keepAlive);
148+
closeSessions = config.closeSessions;
145149
}
146150

147151
public synchronized void start() {
@@ -175,6 +179,11 @@ public synchronized void stop() {
175179
}
176180

177181
for (SelectorThread selector : selectors) {
182+
if (closeSessions) {
183+
for (Session session : selector.selector) {
184+
session.close();
185+
}
186+
}
178187
selector.shutdown();
179188
}
180189

src/one/nio/server/ServerConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class ServerConfig {
3535
public int keepAlive;
3636
public int threadPriority = Thread.NORM_PRIORITY;
3737
public SchedulingPolicy schedulingPolicy;
38+
public boolean closeSessions;
3839

3940
public ServerConfig() {
4041
}
@@ -58,6 +59,7 @@ private ServerConfig(ConnectionString conn) {
5859
this.queueTime = conn.getIntParam("queueTime", 0) / 1000;
5960
this.threadPriority = conn.getIntParam("threadPriority", Thread.NORM_PRIORITY);
6061
this.schedulingPolicy = SchedulingPolicy.valueOf(conn.getStringParam("schedulingPolicy", "OTHER"));
62+
this.closeSessions = conn.getBooleanParam("closeSessions", false);
6163
}
6264

6365
// Do not use for new servers! Use ConfigParser instead

0 commit comments

Comments
 (0)