[jboss-cvs] JBossAS SVN: r108993 - projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Oct 29 11:35:17 EDT 2010


Author: pferraro
Date: 2010-10-29 11:35:17 -0400 (Fri, 29 Oct 2010)
New Revision: 108993

Modified:
   projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainer.java
Log:
Ensure our @CacheStopped listener is invoked last.
Allow more transparency between singleton cache container and parent cache container.

Modified: projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainer.java
===================================================================
--- projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainer.java	2010-10-29 15:33:13 UTC (rev 108992)
+++ projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainer.java	2010-10-29 15:35:17 UTC (rev 108993)
@@ -26,13 +26,12 @@
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
 
 import org.infinispan.Cache;
 import org.infinispan.config.Configuration;
 import org.infinispan.config.GlobalConfiguration;
 import org.infinispan.lifecycle.ComponentStatus;
+import org.infinispan.manager.CacheContainer;
 import org.infinispan.manager.DefaultCacheManager;
 import org.infinispan.manager.EmbeddedCacheManager;
 import org.infinispan.notifications.Listener;
@@ -50,8 +49,6 @@
    private final EmbeddedCacheManager container;
    private final ConcurrentMap<String, EmbeddedCacheManager> containers = new ConcurrentHashMap<String, EmbeddedCacheManager>();
 
-   private ExecutorService executor;
-   
    public DefaultCacheContainer(EmbeddedCacheManager manager)
    {
       this.container = manager;
@@ -60,15 +57,18 @@
    @Override
    public <K, V> Cache<K, V> getCache()
    {
-      return this.container.getCache();
+      return this.getCache(CacheContainer.DEFAULT_CACHE_NAME, this.container.getDefaultConfiguration());
    }
 
    @Override
    public <K, V> Cache<K, V> getCache(String cacheName)
    {
-      // Until ISPN-658 is fixed, we need to create a separate adhoc cache manager if requested cache uses DIST mode.
-      Configuration config = this.container.defineConfiguration(cacheName, new Configuration());
-      
+      return this.getCache(cacheName, this.container.defineConfiguration(cacheName, new Configuration()));
+   }
+   
+   // Until ISPN-658 is fixed, we need to create a separate adhoc cache manager if requested cache uses DIST mode.
+   private <K, V> Cache<K, V> getCache(final String cacheName, Configuration config)
+   {
       if (config.getCacheMode().isDistributed())
       {
          GlobalConfiguration globalConfig = this.container.getGlobalConfiguration();
@@ -108,19 +108,55 @@
             global.setTransportProperties(properties);
             
             // Create single use cache manager
-            container = new DefaultCacheManager(global, config, false);
+            container = new DefaultCacheManager(global, config, false)
+            {
+               /**
+                * {@inheritDoc}
+                * @see org.infinispan.manager.DefaultCacheManager#getCache(java.lang.String)
+                */
+               @Override
+               public <KK, VV> Cache<KK, VV> getCache(String name)
+               {
+                  if (cacheName.equals(name))
+                  {
+                     return super.getCache(CacheContainer.DEFAULT_CACHE_NAME);
+                  }
+                  
+                  return DefaultCacheContainer.this.getCache(cacheName);
+               }
+
+               /**
+                * {@inheritDoc}
+                * @see org.infinispan.manager.DefaultCacheManager#addListener(java.lang.Object)
+                */
+               @Override
+               public void addListener(Object listener)
+               {
+                  super.addListener(listener);
+                  // Warning - very, very hacky...
+                  // Better solution - use custom notifier that allows
+                  // some level of control over listeners order
+                  if (listener != DefaultCacheContainer.this)
+                  {
+                     // Make sure our listener is last since our listener stops the container!
+                     this.removeListener(DefaultCacheContainer.this);
+                     this.addListener(DefaultCacheContainer.this);
+                  }
+               }
+            };
             
             EmbeddedCacheManager existing = this.containers.putIfAbsent(clusterName, container);
             
             if (existing == null)
             {
+               container.addListener(this);
+               
                for (Object listener: this.container.getListeners())
                {
                   container.addListener(listener);
                }
                
                // The cache manager should stop when the cache stops
-               container.addListener(this);
                container.start();
             }
             else
@@ -129,7 +165,7 @@
             }
          }
          
-         return container.getCache();
+         return container.getCache(cacheName);
       }
       
       return this.container.getCache(cacheName);
@@ -138,7 +174,6 @@
    @Override
    public synchronized void start()
    {
-      this.executor = Executors.newSingleThreadExecutor();
       this.container.start();
    }
 
@@ -146,10 +181,6 @@
    public synchronized void stop()
    {
       this.container.stop();
-      if (this.executor != null)
-      {
-         this.executor.shutdown();
-      }
    }
 
    @Override
@@ -237,29 +268,6 @@
       
       this.containers.remove(manager.getClusterName());
       
-      // Stop the cache manager when its cache stops
-      // Need to invoke in separate thread - to avoid RejectedExecutionException of listener
-      Runnable task = new Runnable()
-      {
-         @Override
-         public void run()
-         {
-            // Synchronize to ensure that parent method has completed
-            synchronized (DefaultCacheContainer.this)
-            {
-               manager.stop();
-            }
-         }
-      };
-      
-      // This shouldn't ever be null here - but just in case
-      if (this.executor != null)
-      {
-         this.executor.submit(task);
-      }
-      else
-      {
-         task.run();
-      }
+      manager.stop();
    }
 }



More information about the jboss-cvs-commits mailing list