diff options
author | Greg Wilkins | 2014-07-17 00:50:54 +0000 |
---|---|---|
committer | Greg Wilkins | 2014-07-17 00:50:54 +0000 |
commit | 3a5e67ce9e3be6a4f442a4b699c846cf8f1caeee (patch) | |
tree | 626f45309d9f1e10b3a018ca278b5ce1ebd91501 | |
parent | 591eedcb5965b0fc166d59fb7d8d840e94a2dd81 (diff) | |
download | org.eclipse.jetty.project-3a5e67ce9e3be6a4f442a4b699c846cf8f1caeee.tar.gz org.eclipse.jetty.project-3a5e67ce9e3be6a4f442a4b699c846cf8f1caeee.tar.xz org.eclipse.jetty.project-3a5e67ce9e3be6a4f442a4b699c846cf8f1caeee.zip |
Making ShutdownThread actually stop
-rw-r--r-- | jetty-server/src/main/java/org/eclipse/jetty/server/ShutdownMonitor.java | 47 | ||||
-rw-r--r-- | jetty-server/src/test/java/org/eclipse/jetty/server/ShutdownMonitorTest.java | 76 |
2 files changed, 43 insertions, 80 deletions
diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ShutdownMonitor.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ShutdownMonitor.java index abc6d61ef8..38b866574d 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ShutdownMonitor.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ShutdownMonitor.java @@ -54,20 +54,18 @@ public class ShutdownMonitor } /** - * ShutdownMonitorThread + * ShutdownMonitorRunnable * * Thread for listening to STOP.PORT for command to stop Jetty. * If ShowndownMonitor.exitVm is true, then Sytem.exit will also be * called after the stop. * */ - public class ShutdownMonitorThread extends Thread + private class ShutdownMonitorRunnable implements Runnable { - - public ShutdownMonitorThread () + public ShutdownMonitorRunnable() { - setDaemon(true); - setName("ShutdownMonitor"); + startListenSocket(); } @Override @@ -102,7 +100,7 @@ public class ShutdownMonitor // Graceful Shutdown debug("Issuing graceful shutdown.."); ShutdownThread.getInstance().run(); - + //Stop accepting any more close(serverSocket); serverSocket = null; @@ -148,28 +146,7 @@ public class ShutdownMonitor } } - public void start() - { - if (isAlive()) - { - // TODO why are we reentrant here? - if (DEBUG) - System.err.printf("ShutdownMonitorThread already started"); - return; // cannot start it again - } - - startListenSocket(); - - if (serverSocket == null) - { - return; - } - if (DEBUG) - System.err.println("Starting ShutdownMonitorThread"); - super.start(); - } - - private void startListenSocket() + public void startListenSocket() { if (port < 0) { @@ -217,9 +194,7 @@ public class ShutdownMonitor private String key; private boolean exitVm; private ServerSocket serverSocket; - private ShutdownMonitorThread thread; - - + private Thread thread; /** * Create a ShutdownMonitor using configuration from the System properties. @@ -372,18 +347,20 @@ public class ShutdownMonitor protected void start() throws Exception { - ShutdownMonitorThread t = null; + Thread t = null; + synchronized (this) { if (thread != null && thread.isAlive()) { - // TODO why are we reentrant here? if (DEBUG) System.err.printf("ShutdownMonitorThread already started"); return; // cannot start it again } - thread = new ShutdownMonitorThread(); + thread = new Thread(new ShutdownMonitorRunnable()); + thread.setDaemon(true); + thread.setName("ShutdownMonitor"); t = thread; } diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ShutdownMonitorTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ShutdownMonitorTest.java index bd02b034d8..0b02ee1b8a 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/ShutdownMonitorTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ShutdownMonitorTest.java @@ -26,43 +26,36 @@ import java.io.LineNumberReader; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; +import java.util.concurrent.TimeUnit; import org.junit.Test; /** * ShutdownMonitorTest - * - * - * */ public class ShutdownMonitorTest { - - @Test - public void testShutdown () - throws Exception + public void testShutdown() throws Exception { - - //test port and key assignment + // test port and key assignment ShutdownMonitor.getInstance().setPort(0); ShutdownMonitor.getInstance().setExitVm(false); ShutdownMonitor.getInstance().start(); String key = ShutdownMonitor.getInstance().getKey(); int port = ShutdownMonitor.getInstance().getPort(); - - //try starting a 2nd time (should be ignored) - ShutdownMonitor.getInstance().start(); - - + + // try starting a 2nd time (should be ignored) + ShutdownMonitor.getInstance().start(); + stop(port,key,true); assertTrue(!ShutdownMonitor.getInstance().isAlive()); - - //should be able to change port and key because it is stopped + + // should be able to change port and key because it is stopped ShutdownMonitor.getInstance().setPort(0); - ShutdownMonitor.getInstance().setKey("foo"); + ShutdownMonitor.getInstance().setKey("foo"); ShutdownMonitor.getInstance().start(); - + key = ShutdownMonitor.getInstance().getKey(); port = ShutdownMonitor.getInstance().getPort(); assertTrue(ShutdownMonitor.getInstance().isAlive()); @@ -71,41 +64,34 @@ public class ShutdownMonitorTest assertTrue(!ShutdownMonitor.getInstance().isAlive()); } - - public void stop (int port, String key, boolean check) - throws Exception + public void stop(int port, String key, boolean check) throws Exception { - Socket s = null; - - try + System.out.printf("Attempting stop to localhost:%d (%b)%n",port,check); + try (Socket s = new Socket(InetAddress.getByName("127.0.0.1"),port)) { - //send stop command - s = new Socket(InetAddress.getByName("127.0.0.1"),port); - - OutputStream out = s.getOutputStream(); - out.write((key + "\r\nstop\r\n").getBytes()); - out.flush(); - - if (check) + // send stop command + try (OutputStream out = s.getOutputStream()) { - //wait a little - Thread.currentThread().sleep(600); + out.write((key + "\r\nstop\r\n").getBytes()); + out.flush(); - //check for stop confirmation - LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream())); - String response; - if ((response = lin.readLine()) != null) + if (check) { - assertEquals("Stopped", response); + // wait a little + TimeUnit.MILLISECONDS.sleep(600); + + // check for stop confirmation + LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream())); + String response; + if ((response = lin.readLine()) != null) + { + assertEquals("Stopped",response); + } + else + throw new IllegalStateException("No stop confirmation"); } - else - throw new IllegalStateException("No stop confirmation"); } } - finally - { - if (s != null) s.close(); - } } } |