Author: manik.surtani(a)jboss.com
Date: 2007-10-24 12:09:31 -0400 (Wed, 24 Oct 2007)
New Revision: 4681
Modified:
   core/trunk/src/main/java/org/jboss/cache/CacheImpl.java
Log:
Conditional registration of VM shutdown hook
Modified: core/trunk/src/main/java/org/jboss/cache/CacheImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/CacheImpl.java	2007-10-24 14:42:44 UTC (rev
4680)
+++ core/trunk/src/main/java/org/jboss/cache/CacheImpl.java	2007-10-24 16:09:31 UTC (rev
4681)
@@ -67,6 +67,7 @@
 import org.jgroups.util.RspList;
 import org.jgroups.util.Util;
 
+import javax.management.MBeanServerFactory;
 import javax.transaction.Status;
 import javax.transaction.SystemException;
 import javax.transaction.Transaction;
@@ -830,22 +831,36 @@
 
       notifier.notifyCacheStarted(this, getInvocationContext());
 
-      // install a VM shutdown hook
-      Thread shutdownHook = new Thread()
-      {
-         public void run()
-         {
-            CacheImpl.this.stop();
-         }
-      };
+      addShutdownHook();
 
-      Runtime.getRuntime().addShutdownHook(shutdownHook);
-
       log.info("JBoss Cache version: " + getVersion());
 
       cacheStatus = CacheStatus.STARTED;
    }
 
+   private void addShutdownHook()
+   {
+      ArrayList al = MBeanServerFactory.findMBeanServer(null);
+      if (al.size() == 0)
+      {
+         // 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()
+            {
+               CacheImpl.this.stop();
+            }
+         };
+
+      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.");
+      }
+   }
+
    /**
     * Lifecycle method.
     */
    
    
    
 
                    
                    
                        
                        Show replies by date