Mercurial Hosting > luan
changeset 1142:0f59eab45f3d
webserver - threading
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 30 Jan 2018 18:02:47 -0700 |
parents | 14927c34cbb7 |
children | 3bf5190b3c77 |
files | src/luan/webserver/Connection.java src/luan/webserver/Server.java |
diffstat | 2 files changed, 27 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
diff -r 14927c34cbb7 -r 0f59eab45f3d src/luan/webserver/Connection.java --- a/src/luan/webserver/Connection.java Tue Jan 30 00:55:17 2018 -0700 +++ b/src/luan/webserver/Connection.java Tue Jan 30 18:02:47 2018 -0700 @@ -12,13 +12,16 @@ final class Connection { private static final Logger logger = LoggerFactory.getLogger(Connection.class); + static void handle(Server server,Socket socket) { + new Connection(server,socket).handle(); + } + private final Server server; private final Socket socket; - Connection(Server server,Socket socket) { + private Connection(Server server,Socket socket) { this.server = server; this.socket = socket; - handle(); } private void handle() {
diff -r 14927c34cbb7 -r 0f59eab45f3d src/luan/webserver/Server.java --- a/src/luan/webserver/Server.java Tue Jan 30 00:55:17 2018 -0700 +++ b/src/luan/webserver/Server.java Tue Jan 30 18:02:47 2018 -0700 @@ -3,6 +3,9 @@ import java.io.IOException; import java.net.Socket; import java.net.ServerSocket; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,8 +15,7 @@ public final int port; public final Handler handler; - private volatile boolean isRunning = false; - private Thread thread; + private static final ExecutorService exec = Executors.newCachedThreadPool(); public Server(int port,Handler handler) { this.port = port; @@ -25,32 +27,34 @@ } public synchronized void start() throws IOException { - isRunning = true; final ServerSocket ss = newServerSocket(); - thread = new Thread("luan.webserver") { - public void run() { - try { - while(isRunning) { - Socket socket = ss.accept(); - new Connection(Server.this,socket); - } - } catch(IOException e) { - logger.error("",e); + exec.execute(new Runnable(){public void run() { + try { + while(!exec.isShutdown()) { + final Socket socket = ss.accept(); + exec.execute(new Runnable(){public void run() { + Connection.handle(Server.this,socket); + }}); } + } catch(IOException e) { + logger.error("",e); } - }; - thread.start(); + }}); logger.info("started server on port "+port); } - public synchronized void stop() { - isRunning = false; + public synchronized boolean stop(long timeoutSeconds) { try { - thread.join(); + exec.shutdownNow(); + boolean stopped = exec.awaitTermination(timeoutSeconds,TimeUnit.SECONDS); + if(stopped) + logger.info("stopped server on port "+port); + else + logger.warn("couldn't stop server on port "+port); + return stopped; } catch(InterruptedException e) { throw new RuntimeException(e); } - logger.info("stopped server on port "+port); } }