[vlma-devel] commit: Register a shutdown hook to be sure to stop the daemon properly. ( Adrien Grand )

git version control git at videolan.org
Sun Aug 24 22:37:54 CEST 2008


vlma | branch: master | Adrien Grand <jpountz at videolan.org> | Sun Aug 24 21:10:31 2008 +0200| [349060cf074d5b5d3795f1d37ee60b36f6f147fd] | committer: Adrien Grand 

Register a shutdown hook to be sure to stop the daemon properly.

> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=349060cf074d5b5d3795f1d37ee60b36f6f147fd
---

 .../main/java/org/videolan/vlma/daemon/VLMad.java  |   25 +++++++++++++++++--
 1 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/vlma-daemon/src/main/java/org/videolan/vlma/daemon/VLMad.java b/vlma-daemon/src/main/java/org/videolan/vlma/daemon/VLMad.java
index a907448..53be26a 100644
--- a/vlma-daemon/src/main/java/org/videolan/vlma/daemon/VLMad.java
+++ b/vlma-daemon/src/main/java/org/videolan/vlma/daemon/VLMad.java
@@ -63,12 +63,14 @@ public class VLMad {
         System.out.println("Usage: java org.videolan.vlma.daemon.VLMad (start|reload|stop)");
     }
 
-    public static void dispatch(Command command) {
+    public void dispatch(Command command) {
         if(command.equals(Command.start)) {
             logger.debug("Starting VLMad");
             logger.debug("Loading Spring application context");
             ApplicationContext ac = new ClassPathXmlApplicationContext("daemon.xml");
             Daemon daemon = (Daemon) ac.getBean("daemon");
+            logger.debug("Registering shutdown hook");
+            Runtime.getRuntime().addShutdownHook(new ShutdownHook(daemon));
             daemon.start();
         } else {
             try {
@@ -76,7 +78,6 @@ public class VLMad {
                 Data data = (Data) ac.getBean("dataImporter");
                 if(command.equals(Command.stop)) {
                     data.stop();
-                    ac.close();
                 } else if (command.equals(Command.reload)) {
                     data.reload();
                 }
@@ -103,7 +104,8 @@ public class VLMad {
                 System.exit(1);
             }
         }
-        dispatch(command);
+        VLMad vlmad = new VLMad();
+        vlmad.dispatch(command);
     }
 
     public static URL getDefaultConfigurationFile() {
@@ -141,4 +143,21 @@ public class VLMad {
             return new BaseConfiguration();
         }
     }
+
+    private class ShutdownHook extends Thread {
+
+        private Daemon daemon;
+
+        public ShutdownHook(Daemon daemon) {
+            this.daemon = daemon;
+        }
+
+        public void run() {
+            // This if statement is here to prevent the shutdown hook from
+            // being called twice if Daemon#stop has already been triggered
+            if (daemon.isRunning())
+                daemon.stop();
+        }
+    }
+
 }



More information about the vlma-devel mailing list