[jbosscache-commits] JBoss Cache SVN: r4980 - core/trunk/src/main/java/org/jboss/cache.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Fri Jan 4 08:34:45 EST 2008


Author: manik.surtani at jboss.com
Date: 2008-01-04 08:34:44 -0500 (Fri, 04 Jan 2008)
New Revision: 4980

Modified:
   core/trunk/src/main/java/org/jboss/cache/CacheImpl.java
Log:
JBCACHE-1204 - proper behaviour of shutdown hook when other sources may trigger a shutdown - such as an MBean server, microcontainer, manual shutdown, etc.

Modified: core/trunk/src/main/java/org/jboss/cache/CacheImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/CacheImpl.java	2008-01-04 13:02:24 UTC (rev 4979)
+++ core/trunk/src/main/java/org/jboss/cache/CacheImpl.java	2008-01-04 13:34:44 UTC (rev 4980)
@@ -53,7 +53,6 @@
 import org.jgroups.util.Rsp;
 import org.jgroups.util.RspList;
 
-import javax.management.MBeanServerFactory;
 import javax.transaction.Status;
 import javax.transaction.SystemException;
 import javax.transaction.Transaction;
@@ -183,6 +182,15 @@
    private Interceptor interceptorChain;
 
    private boolean trace;
+   /**
+    * Hook to shut down the cache when the JVM exits.
+    */
+   private Thread shutdownHook;
+   /**
+    * A flag that the shutdown hook sets before calling cache.stop().  Allows stop() to identify if it has been called
+    * from a shutdown hook.
+    */
+   private boolean invokedFromShutdownHook;
 
    /**
     * Constructs an uninitialized CacheImpl.
@@ -769,25 +777,26 @@
 
    private void addShutdownHook()
    {
-      ArrayList al = MBeanServerFactory.findMBeanServer(null);
-      if (al.size() == 0)
+      // *Always* register a shutdown hook.  If cache.stop() is called manually or from an MBean server or microcontainer,
+      // cache.stop will de-register the shutdown hook to prevent shutdown from happening again when the JVM exits.
+
+      shutdownHook = new Thread()
       {
-         // the only MBean server is the system (JDK) server.  So we need to register a shutdown hook.
-         // install a VM shutdown hook
-         Thread shutdownHook = new Thread()
+         public void run()
          {
-            public void run()
+            try
             {
+               invokedFromShutdownHook = true;
                CacheImpl.this.stop();
             }
-         };
+            finally
+            {
+               invokedFromShutdownHook = false;
+            }
+         }
+      };
 
-         Runtime.getRuntime().addShutdownHook(shutdownHook);
-      }
-      else
-      {
-         log.trace("Running in an MBeanServer environment.  Not registering a shutdown hook with the VM as the MBeanServer will handle lifecycle.");
-      }
+      Runtime.getRuntime().addShutdownHook(shutdownHook);
    }
 
    /**
@@ -895,6 +904,9 @@
    {
       cacheStatus = CacheStatus.STOPPING;
 
+      // if this is called from a source other than the shutdown hook, deregister the shutdown hook.      
+      if (!invokedFromShutdownHook) Runtime.getRuntime().removeShutdownHook(shutdownHook);
+
       componentRegistry.stop();
 
       // before closing the channel stop the buddy manager




More information about the jbosscache-commits mailing list