Skip to content

Commit

Permalink
Make sure all threads are shutdown when the game is closed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamebuster19901 committed Jan 27, 2025
1 parent 291345b commit 140ff5d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/main/java/com/wildermods/autosplitter/net/SplitterServer.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.wildermods.autosplitter.net;

import java.net.InetSocketAddress;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.ee10.websocket.server.config.JettyWebSocketServletContainerInitializer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;

import com.wildermods.autosplitter.AutosplitCommandSender;
import com.wildermods.autosplitter.Main;
Expand All @@ -20,6 +22,11 @@ public class SplitterServer extends Server implements AutosplitCommandSender {
public static final String DEFAULT_HOST = "localhost";
public static final int DEFAULT_PORT = 55555;
private static SplitterServer INSTANCE = null;
private final QueuedThreadPool threadPool = new QueuedThreadPool(4);
{
threadPool.setDaemon(true);
}


final CopyOnWriteArrayList<SplitterWebSocket> clients = new CopyOnWriteArrayList<>();

Expand All @@ -45,8 +52,15 @@ public void establishServer(String host, int port) {
LOGGER.catching(e);
}
}
Server server = null;
ScheduledExecutorScheduler scheduler = new ScheduledExecutorScheduler("Splitter Server Scheduler", true);
try {
Server server = new Server(new InetSocketAddress(host, port));
server = new Server(threadPool, scheduler, null);

ServerConnector connector = new ServerConnector(this);
connector.setHost(host);
connector.setPort(port);
super.addConnector(connector);

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
Expand All @@ -62,6 +76,11 @@ public void establishServer(String host, int port) {
catch(Exception e) {
Main.LOGGER.catching(e);
}
finally {
if(server != null) {
server.setStopAtShutdown(true);
}
}
}

public Server getImpl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public class SplitterWebSocket implements AutosplitCommandSender {

private static final Logger LOGGER = new Logger(SplitterWebSocket.class);

private final ScheduledExecutorService heartbeatScheduler = Executors.newScheduledThreadPool(1);
private final ScheduledExecutorService heartbeatScheduler = Executors.newScheduledThreadPool(1, runnable -> {
Thread thread = new Thread("Splitter Heartbeat");
thread.setDaemon(true);
return thread;
});
private Session session;
private String sessionID;

Expand Down

0 comments on commit 140ff5d

Please sign in to comment.