commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1023469 - /commons/proper/daemon/trunk/src/samples/SimpleApplication.java
Date Sun, 17 Oct 2010 11:48:42 GMT
Author: mturk
Date: Sun Oct 17 11:48:41 2010
New Revision: 1023469

URL: http://svn.apache.org/viewvc?rev=1023469&view=rev
Log:
Add ShutdownHook to the example

Modified:
    commons/proper/daemon/trunk/src/samples/SimpleApplication.java

Modified: commons/proper/daemon/trunk/src/samples/SimpleApplication.java
URL: http://svn.apache.org/viewvc/commons/proper/daemon/trunk/src/samples/SimpleApplication.java?rev=1023469&r1=1023468&r2=1023469&view=diff
==============================================================================
--- commons/proper/daemon/trunk/src/samples/SimpleApplication.java (original)
+++ commons/proper/daemon/trunk/src/samples/SimpleApplication.java Sun Oct 17 11:48:41 2010
@@ -79,7 +79,7 @@ public class SimpleApplication implement
 
         /* Dump a message */
         System.err.println("SimpleApplication: loading on port "+port);
-
+        Runtime.getRuntime().addShutdownHook(new ShutdownHook(app));
         /* Set up this simple daemon */
         app.server = new ServerSocket(port);
         app.thread = new Thread(app);
@@ -159,6 +159,26 @@ public class SimpleApplication implement
         }
     }
 
+    public static class ShutdownHook extends Thread
+    {
+        private final SimpleApplication instance;
+
+        public ShutdownHook(SimpleApplication instance)
+        {
+            this.instance = instance;
+        }
+        public void run()
+        {
+            System.out.println("Shutting down");
+            try {
+                instance.stop();
+            }
+            catch (Exception e) {
+                e.printStackTrace(System.err);
+            }
+        }
+    }
+
     public static class Handler implements Runnable {
 
         private final SimpleApplication parent;



Mime
View raw message