qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rob...@apache.org
Subject svn commit: r1292479 - in /qpid/trunk/qpid/java/broker/src: main/java/org/apache/qpid/server/registry/ test/java/org/apache/qpid/server/registry/
Date Wed, 22 Feb 2012 19:50:53 GMT
Author: robbie
Date: Wed Feb 22 19:50:52 2012
New Revision: 1292479

URL: http://svn.apache.org/viewvc?rev=1292479&view=rev
Log:
QPID-3325: unregister the shutdown hook when closing an ApplicationRegistry instance (by means
other than the shutdownhook having run) and tidy up [use of] the close() method.

Modified:
    qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ApplicationRegistry.java
    qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ConfigurationFileApplicationRegistry.java
    qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/registry/ApplicationRegistryShutdownTest.java

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=1292479&r1=1292478&r2=1292479&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
Wed Feb 22 19:50:52 2012
@@ -82,6 +82,8 @@ 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>();
@@ -186,11 +188,6 @@ public abstract class ApplicationRegistr
         _qmfService = qmfService;
     }
 
-    static
-    {
-        Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownService()));
-    }
-
     private static class ShutdownService implements Runnable
     {
         public void run()
@@ -246,6 +243,45 @@ 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;
@@ -273,7 +309,6 @@ public abstract class ApplicationRegistr
                     _logger.info("Shutting down ApplicationRegistry(" + instance + ")");
                 }
                 instance.close();
-                instance.getBroker().getSystem().removeBroker(instance.getBroker());
             }
         }
         catch (Exception e)
@@ -355,6 +390,8 @@ public abstract class ApplicationRegistr
             // Startup complete, so pop the current actor
             CurrentActor.remove();
         }
+
+        addShutdownHook();
     }
 
 
@@ -536,35 +573,56 @@ public abstract class ApplicationRegistr
         }
     }
 
-
     public void close()
     {
         if (_logger.isInfoEnabled())
         {
             _logger.info("Shutting down ApplicationRegistry:" + this);
         }
-        
-        //Stop Statistics Reporting
-        if (_reportingTimer != null)
+
+        //Set the Actor for Broker Shutdown
+        CurrentActor.set(new BrokerActor(getRootMessageLogger()));
+        try
         {
-            _reportingTimer.cancel();
-        }
+            //Stop Statistics Reporting
+            if (_reportingTimer != null)
+            {
+                _reportingTimer.cancel();
+            }
 
-        //Stop incoming connections
-        unbind();
+            //Stop incoming connections
+            unbind();
 
-        //Shutdown virtualhosts
-        close(_virtualHostRegistry);
+            //Shutdown virtualhosts
+            close(_virtualHostRegistry);
 
-        close(_authenticationManager);
+            close(_authenticationManager);
 
-        close(_qmfService);
+            close(_qmfService);
 
-        close(_pluginManager);
+            close(_pluginManager);
 
-        close(_managedObjectRegistry);
+            close(_managedObjectRegistry);
 
-        CurrentActor.get().message(BrokerMessages.STOPPED());
+            BrokerConfig broker = getBroker();
+            if(broker != null)
+            {
+                broker.getSystem().removeBroker(broker);
+            }
+
+            CurrentActor.get().message(BrokerMessages.STOPPED());
+        }
+        finally
+        {
+            try
+            {
+                CurrentActor.remove();
+            }
+            finally
+            {
+                removeShutdownHook();
+            }
+        }
     }
 
     private void unbind()

Modified: qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ConfigurationFileApplicationRegistry.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ConfigurationFileApplicationRegistry.java?rev=1292479&r1=1292478&r2=1292479&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ConfigurationFileApplicationRegistry.java
(original)
+++ qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ConfigurationFileApplicationRegistry.java
Wed Feb 22 19:50:52 2012
@@ -25,8 +25,6 @@ import org.osgi.framework.BundleContext;
 
 import org.apache.qpid.AMQException;
 import org.apache.qpid.server.configuration.ServerConfiguration;
-import org.apache.qpid.server.logging.actors.BrokerActor;
-import org.apache.qpid.server.logging.actors.CurrentActor;
 import org.apache.qpid.server.management.JMXManagedObjectRegistry;
 import org.apache.qpid.server.management.NoopManagedObjectRegistry;
 
@@ -45,22 +43,6 @@ public class ConfigurationFileApplicatio
     }
 
     @Override
-    public void close()
-    {
-        //Set the Actor for Broker Shutdown
-        CurrentActor.set(new BrokerActor(getRootMessageLogger()));
-        try
-        {
-            super.close();
-        }
-        finally
-        {
-            CurrentActor.remove();
-        }
-    }
-
-
-    @Override
     protected void initialiseManagedObjectRegistry() throws AMQException
     {
         if (getConfiguration().getManagementEnabled())

Modified: qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/registry/ApplicationRegistryShutdownTest.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/registry/ApplicationRegistryShutdownTest.java?rev=1292479&r1=1292478&r2=1292479&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/registry/ApplicationRegistryShutdownTest.java
(original)
+++ qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/registry/ApplicationRegistryShutdownTest.java
Wed Feb 22 19:50:52 2012
@@ -80,11 +80,10 @@ public class ApplicationRegistryShutdown
             }
         }
 
-        // Not using isEmpty as that is not in Java 5
-        assertTrue("No new SASL mechanisms added by initialisation.", additions.size() !=
0 );
+        assertFalse("No new SASL mechanisms added by initialisation.", additions.isEmpty());
 
         //Close the registry which will perform the close the AuthenticationManager
-        getRegistry().close();
+        stopBroker();
 
         //Validate that the SASL plugFins have been removed.
         Provider[] providersAfterClose = Security.getProviders();



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


Mime
View raw message