Author: ningjiang
Date: Tue Jan 13 18:44:56 2009
New Revision: 734309

URL: http://svn.apache.org/viewvc?rev=734309&view=rev
Log:
CAMEL-1244 applied patch with thanks to Hari

Modified:
    
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java

Modified: 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java?rev=734309&r1=734308&r2=734309&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
 (original)
+++ 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
 Tue Jan 13 18:44:56 2009
@@ -28,6 +28,8 @@
 import org.apache.camel.spring.handler.CamelNamespaceHandler;
 import org.apache.camel.util.MainSupport;
 import org.apache.camel.view.ModelFileGenerator;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -65,10 +67,35 @@
         });
 
     }
+    
+    /**
+     * A class for intercepting the hang up signal and do a graceful shutdown 
of
+     * the Camel.
+     */
+    private class HangupInterceptor extends Thread {
+        Log log = LogFactory.getLog(this.getClass());
+        Main mainInstance;
+
+        public HangupInterceptor(Main main) {
+            mainInstance = main;
+        }
+
+        @Override
+        public void run() {
+            log.info("Recieved hang up - stopping the main instance.");
+            try {
+                mainInstance.stop();
+            } catch (Exception ex) {
+                log.warn(ex);
+            }
+        }
+    }
+
 
     public static void main(String... args) {
         Main main = new Main();
         instance = main;
+        main.enableHangupSupport();
         main.run(args);
     }
 
@@ -80,6 +107,15 @@
     public static Main getInstance() {
         return instance;
     }
+    
+    /**
+     * Enables the hangup support. Gracefully stops by calling stop() on a
+     * Hangup signal.
+     */
+    public void enableHangupSupport() {
+        HangupInterceptor interceptor = new HangupInterceptor(this);
+        Runtime.getRuntime().addShutdownHook(interceptor);
+    }
 
     @Override
     public void enableDebug() {


Reply via email to