[jboss-cvs] JBossAS SVN: r109220 - in projects/cluster/ha-server-ispn/trunk/src: test/java/org/jboss/ha/ispn and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Nov 10 00:27:32 EST 2010


Author: pferraro
Date: 2010-11-10 00:27:32 -0500 (Wed, 10 Nov 2010)
New Revision: 109220

Modified:
   projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainerRegistry.java
   projects/cluster/ha-server-ispn/trunk/src/test/java/org/jboss/ha/ispn/DefaultCacheContainerRegistryTest.java
Log:
Unregister cache mbean on cache stop - infinispan doesn't do this until cache container stop.

Modified: projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainerRegistry.java
===================================================================
--- projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainerRegistry.java	2010-11-10 05:01:07 UTC (rev 109219)
+++ projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainerRegistry.java	2010-11-10 05:27:32 UTC (rev 109220)
@@ -21,20 +21,28 @@
  */
 package org.jboss.ha.ispn;
 
+import java.lang.management.ManagementFactory;
 import java.util.Collections;
 import java.util.List;
+import java.util.Locale;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.atomic.AtomicReference;
 
+import javax.management.JMException;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.Name;
 import javax.naming.NameNotFoundException;
 import javax.naming.NamingException;
 
+import org.infinispan.config.Configuration;
 import org.infinispan.config.GlobalConfiguration;
+import org.infinispan.jmx.CacheJmxRegistration;
+import org.infinispan.jmx.ComponentsJmxRegistration;
 import org.infinispan.manager.EmbeddedCacheManager;
 import org.infinispan.notifications.Listener;
 import org.infinispan.notifications.cachemanagerlistener.annotation.CacheStarted;
@@ -52,12 +60,14 @@
  * All cache containers in the registry are bound to jndi.
  * @author Paul Ferraro
  */
+ at Listener
 public class DefaultCacheContainerRegistry implements CacheContainerRegistry
 {
    public static final String DEFAULT_CACHE_MANAGER_NAME = new GlobalConfiguration().getCacheManagerName();
    
    static Logger logger = Logger.getLogger(DefaultCacheContainerRegistry.class);
    
+   // Used to provide access to registry where injection is not an option
    private static final AtomicReference<CacheContainerRegistry> singleton = new AtomicReference<CacheContainerRegistry>();
    public static CacheContainerRegistry getInstance()
    {
@@ -69,8 +79,8 @@
    private final ConcurrentMap<String, String> aliases = new ConcurrentHashMap<String, String>();
    private final ConcurrentMap<String, CacheContainerRegistryEntry> containers = new ConcurrentHashMap<String, CacheContainerRegistryEntry>();
    
+   private volatile MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    private volatile String jndiNamePrefix = null;
-   
    private volatile EmbeddedCacheManager defaultContainer;
    
    /**
@@ -94,6 +104,11 @@
       this.jndiNamePrefix = jndiNamePrefix;
    }
    
+   public void setMBeanServer(MBeanServer server)
+   {
+      this.server = server;
+   }
+   
    public void start() throws Exception
    {
       CacheContainerRegistryConfiguration registry = this.source.getRegistryConfiguration();
@@ -234,7 +249,7 @@
    {
       EmbeddedCacheManager container = entry.getContainer();
       container.start();
-      container.addListener(entry);
+      container.addListener(this);
       
       String jndiName = entry.getJndiName();
       
@@ -270,7 +285,7 @@
       }
       
       EmbeddedCacheManager container = entry.getContainer();
-      container.removeListener(entry);
+      container.removeListener(this);
       container.stop();
    }
    
@@ -304,15 +319,57 @@
       NonSerializableFactory.unbind(name);
    }
    
-   @Listener
-   public static class CacheContainerRegistryEntry
+   @CacheStarted
+   public void cacheStarted(CacheStartedEvent event)
    {
+      this.log("Started", event.getCacheName(), event.getCacheManager());
+   }
+   
+   @CacheStopped
+   public void cacheStopped(CacheStoppedEvent event)
+   {
+      String cacheName = event.getCacheName();
+      EmbeddedCacheManager container = event.getCacheManager();
+      
+      this.log("Stopped", cacheName, container);
+
+      // Infinispan does not unregister cache mbean when cache stops (only when cache manager is stopped), so do it now to avoid classloader leaks
+      Configuration configuration = container.defineConfiguration(cacheName, new Configuration());
+      if (configuration.isExposeJmxStatistics())
+      {
+         GlobalConfiguration global = configuration.getGlobalConfiguration();
+         String jmxCacheName = String.format("%s(%s)", cacheName, configuration.getCacheModeString().toLowerCase(Locale.ENGLISH));
+         try
+         {
+            // Fragile code alert!
+            ObjectName name = ObjectName.getInstance(String.format("%s:%s,%s=%s,manager=%s,%s=%s", global.getJmxDomain(), CacheJmxRegistration.CACHE_JMX_GROUP, ComponentsJmxRegistration.NAME_KEY, ObjectName.quote(jmxCacheName), ObjectName.quote(global.getCacheManagerName()), ComponentsJmxRegistration.COMPONENT_KEY, "Cache"));
+
+            if (this.server.isRegistered(name))
+            {
+               this.server.unregisterMBean(name);
+               logger.debug(String.format("Unregistered cache mbean: %s", name));
+            }
+         }
+         catch (JMException e)
+         {
+            logger.warn(String.format("Failed to unregister mbean for %s cache", cacheName), e);
+         }
+      }
+   }
+   
+   private void log(String event, String cacheName, EmbeddedCacheManager container)
+   {
+      logger.info(String.format("%s \"%s\" cache from \"%s\" container", event, cacheName, container.getGlobalConfiguration().getCacheManagerName()));
+   }
+   
+   static class CacheContainerRegistryEntry
+   {
       private final String id;
       private final List<String> aliases;
       private final String jndiName;
       private final EmbeddedCacheManager container;
       
-      public CacheContainerRegistryEntry(String id, EmbeddedCacheManager container, String jndiName, List<String> aliases)
+      CacheContainerRegistryEntry(String id, EmbeddedCacheManager container, String jndiName, List<String> aliases)
       {
          this.id = id;
          this.container = container;
@@ -339,22 +396,5 @@
       {
          return this.aliases;
       }
-      
-      @CacheStarted
-      public void cacheStarted(CacheStartedEvent event)
-      {
-         this.log("Started", event.getCacheName());
-      }
-      
-      @CacheStopped
-      public void cacheStopped(CacheStoppedEvent event)
-      {
-         this.log("Stopped", event.getCacheName());
-      }
-      
-      private void log(String event, String cacheName)
-      {
-         logger.info(String.format("%s \"%s\" cache from \"%s\" container", event, cacheName, this.id));
-      }
    }
 }

Modified: projects/cluster/ha-server-ispn/trunk/src/test/java/org/jboss/ha/ispn/DefaultCacheContainerRegistryTest.java
===================================================================
--- projects/cluster/ha-server-ispn/trunk/src/test/java/org/jboss/ha/ispn/DefaultCacheContainerRegistryTest.java	2010-11-10 05:01:07 UTC (rev 109219)
+++ projects/cluster/ha-server-ispn/trunk/src/test/java/org/jboss/ha/ispn/DefaultCacheContainerRegistryTest.java	2010-11-10 05:27:32 UTC (rev 109220)
@@ -133,7 +133,7 @@
       EasyMock.expect(factory.createCacheContainer(EasyMock.same(defaultConfig))).andReturn(defaultContainer);
       
       defaultContainer.start();
-      defaultContainer.addListener(EasyMock.anyObject());
+      defaultContainer.addListener(EasyMock.same(registry));
       
       EasyMock.expect(context.getNameParser("")).andReturn(parser);
       EasyMock.expect(parser.parse("java:CacheManager/default")).andReturn(name);
@@ -146,7 +146,7 @@
       EasyMock.expect(factory.createCacheContainer(EasyMock.same(otherConfig))).andReturn(otherContainer);
       
       otherContainer.start();
-      otherContainer.addListener(EasyMock.anyObject());
+      otherContainer.addListener(EasyMock.same(registry));
       
       EasyMock.expect(context.getNameParser("")).andReturn(parser);
       EasyMock.expect(parser.parse("java:CacheManager/other")).andReturn(name);
@@ -192,14 +192,12 @@
       
       this.control.reset();
       
-      Capture<DefaultCacheContainerRegistry.CacheContainerRegistryEntry> capturedEntries = new Capture<DefaultCacheContainerRegistry.CacheContainerRegistryEntry>(CaptureType.ALL);
-      
       context.unbind("java:CacheManager/default");
       defaultContainer.stop();
-      defaultContainer.removeListener(EasyMock.capture(capturedEntries));
+      defaultContainer.removeListener(EasyMock.same(registry));
       context.unbind("java:CacheManager/other");
       otherContainer.stop();
-      otherContainer.removeListener(EasyMock.capture(capturedEntries));
+      otherContainer.removeListener(EasyMock.same(registry));
       
       this.control.replay();
       
@@ -211,11 +209,6 @@
       Assert.assertNull(NonSerializableFactory.lookup("java:CacheManager/default"));
       Assert.assertNull(NonSerializableFactory.lookup("java:CacheManager/other"));
       
-      List<DefaultCacheContainerRegistry.CacheContainerRegistryEntry> entries = capturedEntries.getValues();
-      
-      Assert.assertSame(defaultContainer, entries.get(0).getContainer());
-      Assert.assertSame(otherContainer, entries.get(1).getContainer());
-      
       this.control.reset();
    }
    
@@ -264,7 +257,7 @@
       EasyMock.expect(factory.createCacheContainer(EasyMock.same(otherConfig))).andReturn(otherContainer);
       
       otherContainer.start();
-      otherContainer.addListener(EasyMock.anyObject());
+      otherContainer.addListener(EasyMock.same(registry));
       
       EasyMock.expect(context.getNameParser("")).andReturn(parser);
       EasyMock.expect(parser.parse("java:other")).andReturn(name);
@@ -274,7 +267,7 @@
       EasyMock.expect(factory.createCacheContainer(EasyMock.same(defaultConfig))).andReturn(defaultContainer);
       
       defaultContainer.start();
-      defaultContainer.addListener(EasyMock.anyObject());
+      defaultContainer.addListener(EasyMock.same(registry));
       
       EasyMock.expect(context.getNameParser("")).andReturn(parser);
       EasyMock.expect(parser.parse("java:Infinispan/default")).andReturn(name);
@@ -322,14 +315,12 @@
       
       this.control.reset();
       
-      Capture<DefaultCacheContainerRegistry.CacheContainerRegistryEntry> capturedEntries = new Capture<DefaultCacheContainerRegistry.CacheContainerRegistryEntry>(CaptureType.ALL);
-      
       context.unbind("java:Infinispan/default");
       defaultContainer.stop();
-      defaultContainer.removeListener(EasyMock.capture(capturedEntries));
+      defaultContainer.removeListener(EasyMock.same(registry));
       context.unbind("java:other");
       otherContainer.stop();
-      otherContainer.removeListener(EasyMock.capture(capturedEntries));
+      otherContainer.removeListener(EasyMock.same(registry));
       
       this.control.replay();
       
@@ -341,11 +332,6 @@
       Assert.assertNull(NonSerializableFactory.lookup("java:other"));
       Assert.assertNull(NonSerializableFactory.lookup("java:Infinispan/default"));
       
-      List<DefaultCacheContainerRegistry.CacheContainerRegistryEntry> entries = capturedEntries.getValues();
-      
-      Assert.assertSame(defaultContainer, entries.get(0).getContainer());
-      Assert.assertSame(otherContainer, entries.get(1).getContainer());
-      
       this.control.reset();
    }
 }



More information about the jboss-cvs-commits mailing list