From 53cb3dda73af394d2941987a6096ead73275d9a9 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 28 Aug 2013 10:36:28 +0200 Subject: [PATCH] Modified Server class to be embedded, fixes #10 and #11 --- .../org/dna/mqtt/moquette/server/Server.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/broker/src/main/java/org/dna/mqtt/moquette/server/Server.java b/broker/src/main/java/org/dna/mqtt/moquette/server/Server.java index 3a94ca55..5c658f28 100644 --- a/broker/src/main/java/org/dna/mqtt/moquette/server/Server.java +++ b/broker/src/main/java/org/dna/mqtt/moquette/server/Server.java @@ -38,11 +38,19 @@ public class Server { //private ExecutorService m_notifierPool/* = Executors.newFixedThreadPool(NOTIFIER_POOL_SIZE)*/; public static void main(String[] args) throws IOException { - new Server().startServer(); + final Server server = new Server(); + server.startServer(); + //Bind a shutdown hook + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + server.stopServer(); + } + }); } - protected void startServer() throws IOException { + public void startServer() throws IOException { DemuxingProtocolDecoder decoder = new DemuxingProtocolDecoder(); decoder.addMessageDecoder(new ConnectDecoder()); decoder.addMessageDecoder(new PublishDecoder()); @@ -92,16 +100,10 @@ public class Server { m_acceptor.bind( new InetSocketAddress(Constants.PORT) ); LOG.info("Server binded"); - //Bind a shutdown hook - Runtime.getRuntime().addShutdownHook(new Thread() { - @Override - public void run() { - stopServer(); - } - }); + } - protected void stopServer() { + public void stopServer() { LOG.info("Server stopping..."); messaging.stop();