Author: robbie
Date: Thu Feb 23 14:20:07 2012
New Revision: 1292811

URL: http://svn.apache.org/viewvc?rev=1292811&view=rev
Log:
QPID-3325: move shutdown hook handling into Broker, set the thread name

Modified:
    qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java
    
qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ApplicationRegistry.java

Modified: 
qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java?rev=1292811&r1=1292810&r2=1292811&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java 
(original)
+++ 
qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java 
Thu Feb 23 14:20:07 2012
@@ -20,6 +20,7 @@
  */
 package org.apache.qpid.server;
 
+import org.apache.log4j.Logger;
 import org.apache.log4j.PropertyConfigurator;
 import org.apache.log4j.xml.QpidLog4JConfigurator;
 
@@ -59,8 +60,11 @@ import java.util.Set;
 
 public class Broker
 {
+    private static final Logger LOGGER = Logger.getLogger(Broker.class);
+
     private static final int IPV4_ADDRESS_LENGTH = 4;
     private static final char IPV4_LITERAL_SEPARATOR = '.';
+    private volatile Thread _shutdownHookThread;
 
     protected static class InitException extends RuntimeException
     {
@@ -74,7 +78,14 @@ public class Broker
 
     public void shutdown()
     {
-        ApplicationRegistry.remove();
+        try
+        {
+            removeShutdownHook();
+        }
+        finally
+        {
+            ApplicationRegistry.remove();
+        }
     }
 
     public void startup() throws Exception
@@ -88,6 +99,7 @@ public class Broker
         {
             CurrentActor.set(new BrokerActor(new SystemOutMessageLogger()));
             startupImpl(options);
+            addShutdownHook();
         }
         finally
         {
@@ -441,4 +453,56 @@ public class Broker
 
         blm.register();
     }
+
+    private void addShutdownHook()
+    {
+        Thread shutdownHookThread = new Thread(new ShutdownService());
+        shutdownHookThread.setName("QpidBrokerShutdownHook");
+
+        Runtime.getRuntime().addShutdownHook(shutdownHookThread);
+        _shutdownHookThread = shutdownHookThread;
+
+        LOGGER.debug("Added shutdown hook");
+    }
+
+    private void removeShutdownHook()
+    {
+        Thread shutdownThread = _shutdownHookThread;
+
+        //if there is a shutdown thread and we aren't it, we should remove it
+        if(shutdownThread != null && !(Thread.currentThread() == 
shutdownThread))
+        {
+            LOGGER.debug("Removing shutdown hook");
+
+            _shutdownHookThread = null;
+
+            boolean removed = false;
+            try
+            {
+                removed = 
Runtime.getRuntime().removeShutdownHook(shutdownThread);
+            }
+            catch(IllegalStateException ise)
+            {
+                //ignore, means the JVM is already shutting down
+            }
+
+            if(LOGGER.isDebugEnabled())
+            {
+                LOGGER.debug("Removed shutdown hook: " + removed);
+            }
+        }
+        else
+        {
+            LOGGER.debug("Skipping shutdown hook removal as there either isnt 
one, or we are it.");
+        }
+    }
+
+    private class ShutdownService implements Runnable
+    {
+        public void run()
+        {
+            LOGGER.debug("Shutdown hook running");
+            Broker.this.shutdown();
+        }
+    }
 }

Modified: 
qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ApplicationRegistry.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ApplicationRegistry.java?rev=1292811&r1=1292810&r2=1292811&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ApplicationRegistry.java
 (original)
+++ 
qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ApplicationRegistry.java
 Thu Feb 23 14:20:07 2012
@@ -82,8 +82,6 @@ public abstract class ApplicationRegistr
 
     private static AtomicReference<IApplicationRegistry> _instance = new 
AtomicReference<IApplicationRegistry>(null);
 
-    private volatile Thread _shutdownHookThread;
-
     private final ServerConfiguration _configuration;
 
     private final Map<InetSocketAddress, QpidAcceptor> _acceptors = new 
HashMap<InetSocketAddress, QpidAcceptor>();
@@ -188,14 +186,6 @@ public abstract class ApplicationRegistr
         _qmfService = qmfService;
     }
 
-    private static class ShutdownService implements Runnable
-    {
-        public void run()
-        {
-            remove();
-        }
-    }
-
     public static void initialise(IApplicationRegistry instance) throws 
Exception
     {
         if(instance == null)
@@ -243,45 +233,6 @@ public abstract class ApplicationRegistr
         }
     }
 
-    private void addShutdownHook()
-    {
-        Thread shutdownHookThread = new Thread(new ShutdownService());
-        Runtime.getRuntime().addShutdownHook(shutdownHookThread);
-        _shutdownHookThread = shutdownHookThread;
-    }
-
-    private void removeShutdownHook()
-    {
-        Thread shutdownThread = _shutdownHookThread;
-
-        //if there is a shutdown thread and we aren't it, we should remove it
-        if(shutdownThread != null && !(Thread.currentThread() == 
shutdownThread))
-        {
-            _logger.debug("Removing shutdown hook");
-
-            _shutdownHookThread = null;
-
-            boolean removed = false;
-            try
-            {
-                removed = 
Runtime.getRuntime().removeShutdownHook(shutdownThread);
-            }
-            catch(IllegalStateException ise)
-            {
-                //ignore, means the JVM is already shutting down
-            }
-
-            if(_logger.isDebugEnabled())
-            {
-                _logger.debug("Removed shutdown hook: " + removed);
-            }
-        }
-        else
-        {
-            _logger.debug("Skipping shutdown hook removal as there either isnt 
one, or we are it.");
-        }
-    }
-
     public ConfigStore getConfigStore()
     {
         return _configStore;
@@ -390,8 +341,6 @@ public abstract class ApplicationRegistr
             // Startup complete, so pop the current actor
             CurrentActor.remove();
         }
-
-        addShutdownHook();
     }
 
 
@@ -614,14 +563,7 @@ public abstract class ApplicationRegistr
         }
         finally
         {
-            try
-            {
-                CurrentActor.remove();
-            }
-            finally
-            {
-                removeShutdownHook();
-            }
+            CurrentActor.remove();
         }
     }
 



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:commits-subscr...@qpid.apache.org

Reply via email to