[jboss-cvs] JBossAS SVN: r108010 - in branches/infinispan-int/testsuite/src/main/org/jboss/test/cluster/clusteredentity: classloader and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Sep 7 10:55:57 EDT 2010


Author: pferraro
Date: 2010-09-07 10:55:57 -0400 (Tue, 07 Sep 2010)
New Revision: 108010

Modified:
   branches/infinispan-int/testsuite/src/main/org/jboss/test/cluster/clusteredentity/EntityTestBean.java
   branches/infinispan-int/testsuite/src/main/org/jboss/test/cluster/clusteredentity/classloader/EntityQueryTestBean.java
Log:
Restore cache listeners.

Modified: branches/infinispan-int/testsuite/src/main/org/jboss/test/cluster/clusteredentity/EntityTestBean.java
===================================================================
--- branches/infinispan-int/testsuite/src/main/org/jboss/test/cluster/clusteredentity/EntityTestBean.java	2010-09-07 14:55:34 UTC (rev 108009)
+++ branches/infinispan-int/testsuite/src/main/org/jboss/test/cluster/clusteredentity/EntityTestBean.java	2010-09-07 14:55:57 UTC (rev 108010)
@@ -22,8 +22,8 @@
 package org.jboss.test.cluster.clusteredentity;
 
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.Set;
+import java.util.concurrent.ConcurrentSkipListSet;
 
 import javax.annotation.PreDestroy;
 import javax.ejb.Remote;
@@ -32,13 +32,12 @@
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 
-import org.infinispan.Cache;
-import org.infinispan.manager.CacheContainer;
+import org.infinispan.notifications.Listener;
 import org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited;
 import org.infinispan.notifications.cachelistener.event.CacheEntryVisitedEvent;
-import org.infinispan.tree.Fqn;
+import org.infinispan.notifications.cachemanagerlistener.annotation.CacheStarted;
+import org.infinispan.notifications.cachemanagerlistener.event.CacheStartedEvent;
 import org.jboss.ejb3.annotation.RemoteBinding;
-import org.jboss.ha.ispn.CacheContainerRegistry;
 import org.jboss.ha.ispn.DefaultCacheContainerRegistry;
 import org.jboss.logging.Logger;
 
@@ -58,10 +57,6 @@
    @PersistenceContext
    private EntityManager manager;
    
-   private String cacheConfigName;
-   
-   private transient Cache<Object, Object> cache;
-   
    private transient MyListener listener;
 
    public EntityTestBean()
@@ -70,28 +65,11 @@
    
    public void getCache(String cacheConfigName)
    {
-      this.cacheConfigName = cacheConfigName;
-
-      try
+      if (listener == null)
       {
-         if (cache == null && cacheConfigName != null)
-         {
-            CacheContainerRegistry cr = DefaultCacheContainerRegistry.getInstance();
-            CacheContainer cc = cr.getCacheContainer("entity");
-            cache = cc.getCache(cacheConfigName);
-            cache.start();
-         }
-         
-         if (listener == null)
-         {
-            listener = new MyListener();
-            cache.addListener(listener);
-         }
+         listener = new MyListener();
+         DefaultCacheContainerRegistry.getInstance().getCacheContainer("entity").addListener(listener);
       }
-      catch (Exception e)
-      {
-         throw new RuntimeException(e);
-      }
    }
    
    public Customer createCustomer()
@@ -190,16 +168,9 @@
    @Remove
    public void cleanup()
    {
-      try
-      {         
-         if (cache != null && listener != null)
-         {
-            cache.removeListener(listener);
-         }
-      }
-      catch (Exception e)
+      if (listener != null)
       {
-         log.error("Caught exception in cleanup", e);
+         DefaultCacheContainerRegistry.getInstance().getCacheContainer("entity").removeListener(listener);
       }
       
       try
@@ -209,9 +180,10 @@
             Customer c = findByCustomerId(new Integer(1));
             if (c != null)
             {
-               Set contacts = c.getContacts();
-               for (Iterator it = contacts.iterator(); it.hasNext();)
-                  manager.remove(it.next());
+               for (Contact contact: c.getContacts())
+               {
+                  manager.refresh(contact);
+               }
                c.setContacts(null);
                manager.remove(c);
             }
@@ -221,44 +193,32 @@
       {
          log.error("Caught exception in cleanup", e);
       }
-      
-      try
-      {
-         if (cache != null)
-            cache.stop();
-      }
-      catch (Exception e)
-      {
-         log.error("Caught exception releasing cache", e);
-      }
    }
-// TODO:  change the below listening code into something that works for Infinispan
+
+   @Listener
    public class MyListener
    {
-      HashSet<String> visited = new HashSet<String>(); 
+      Set<String> visited = new ConcurrentSkipListSet<String>(); 
       
       public void clear()
       {
          visited.clear();
       }
       
+      @CacheStarted
+      public void cacheStarted(CacheStartedEvent event)
+      {
+         event.getCacheManager().getCache(event.getCacheName()).addListener(this);
+      }
+      
       @CacheEntryVisited
-      public void nodeVisited(CacheEntryVisitedEvent event)
+      public void entryVisited(CacheEntryVisitedEvent event)
       {
          if (!event.isPre())
          {
-//            Fqn fqn = event.getFqn();
-//            System.out.println("MyListener - Visiting node " + fqn.toString());
-//            String name = fqn.toString();
-//            String token = ".clusteredentity.";
-//            int index = name.indexOf(token);
-//            if (index > -1)
-//            {
-//               index += token.length();
-//               name = name.substring(index);
-//               System.out.println("MyListener - recording visit to " + name);
-//               visited.add(name);
-//            }
+            String region = event.getCache().getName();
+            System.out.println("MyListener - recording visit to " + region);
+            visited.add(region);
          }
       }
    }

Modified: branches/infinispan-int/testsuite/src/main/org/jboss/test/cluster/clusteredentity/classloader/EntityQueryTestBean.java
===================================================================
--- branches/infinispan-int/testsuite/src/main/org/jboss/test/cluster/clusteredentity/classloader/EntityQueryTestBean.java	2010-09-07 14:55:34 UTC (rev 108009)
+++ branches/infinispan-int/testsuite/src/main/org/jboss/test/cluster/clusteredentity/classloader/EntityQueryTestBean.java	2010-09-07 14:55:57 UTC (rev 108010)
@@ -21,10 +21,10 @@
  */
 package org.jboss.test.cluster.clusteredentity.classloader;
 
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.ConcurrentSkipListSet;
 
 import javax.annotation.PreDestroy;
 import javax.ejb.Remote;
@@ -34,16 +34,16 @@
 import javax.persistence.PersistenceContext;
 import javax.persistence.Query;
 
-import org.infinispan.Cache;
-import org.infinispan.manager.CacheContainer;
+import org.infinispan.notifications.Listener;
 import org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated;
 import org.infinispan.notifications.cachelistener.annotation.CacheEntryModified;
 import org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited;
 import org.infinispan.notifications.cachelistener.event.CacheEntryCreatedEvent;
 import org.infinispan.notifications.cachelistener.event.CacheEntryModifiedEvent;
 import org.infinispan.notifications.cachelistener.event.CacheEntryVisitedEvent;
+import org.infinispan.notifications.cachemanagerlistener.annotation.CacheStarted;
+import org.infinispan.notifications.cachemanagerlistener.event.CacheStartedEvent;
 import org.jboss.ejb3.annotation.RemoteBinding;
-import org.jboss.ha.ispn.CacheContainerRegistry;
 import org.jboss.ha.ispn.DefaultCacheContainerRegistry;
 import org.jboss.logging.Logger;
 
@@ -63,10 +63,6 @@
    @PersistenceContext
    private EntityManager manager;
    
-   private String cacheConfigName;
-   
-   private transient Cache<Object, Object> cache;
-   
    private MyListener listener;
 
    public EntityQueryTestBean()
@@ -75,17 +71,10 @@
    
    public void getCache(boolean optimistic)
    {
-      if (optimistic)
-         cacheConfigName = "optimistic-shared";
-      else
-         cacheConfigName = "pessimistic-shared";
-
       try
       {
-         //Just to initialise the cache with a listener
-         Cache<Object, Object> cache = getCache();
          listener = new MyListener();
-         cache.addListener(listener);
+         DefaultCacheContainerRegistry.getInstance().getCacheContainer("entity").addListener(listener);
       }
       catch (Exception e)
       {
@@ -221,32 +210,14 @@
    
    public boolean getSawRegionModification(String regionName)
    {
-//      return getSawRegion(regionName, listener.modified);
-      return false;
+      return listener.modified.remove(regionName);
    }
    
    public boolean getSawRegionAccess(String regionName)
    {
-//      return getSawRegion(regionName, listener.accessed);
-      return false;
+      return listener.accessed.remove(regionName);
    }
    
-//   private boolean getSawRegion(String regionName, Set<Fqn<String>> sawEvent)
-//   {
-//      boolean saw = false;
-//      for (Iterator<Fqn<String>> it = sawEvent.iterator(); it.hasNext();)
-//      {
-//         Fqn<String> modified = (Fqn<String>) it.next();
-//         if (modified.toString().indexOf(regionName) > -1)
-//         {
-//            it.remove();
-//            saw = true;
-//         }
-//      }
-//      return saw;
-//
-//   }
-   
    public void cleanup()
    {
       internalCleanup();
@@ -293,80 +264,63 @@
       try
       {
          listener.clear();
-         getCache().removeListener(listener);
+         DefaultCacheContainerRegistry.getInstance().getCacheContainer("entity").removeListener(listener);
       }
       catch (Exception e)
       {
         log.error("Caught exception in remove", e);
       }
-      
-      try
-      {
-         if (cache != null)
-            cache.stop();
-      }
-      catch (Exception e)
-      {
-         log.error("Caught exception releasing cache", e);
-      }
    }
 
-   private Cache<Object, Object> getCache() throws Exception
-   {
-      if (cache == null && cacheConfigName != null)
-      {
-         CacheContainerRegistry cr = DefaultCacheContainerRegistry.getInstance();
-         CacheContainer cc = cr.getCacheContainer("entity");
-         cache = cc.getCache(cacheConfigName);
-         cache.start();
-
-      }
-      return cache;
-   }
-// TODO:  the below listener code is commented out, replace it with something that works with our use of Infinispan
+   @Listener
    public class MyListener
    {
-//      HashSet<Fqn<String>> modified = new HashSet<Fqn<String>>();
-//      HashSet<Fqn<String>> accessed = new HashSet<Fqn<String>>();
+      Set<String> modified = new ConcurrentSkipListSet<String>();
+      Set<String> accessed = new ConcurrentSkipListSet<String>();
       
       public void clear()
       {
-//         modified.clear();
-//         accessed.clear();
+         modified.clear();
+         accessed.clear();
       }
       
+      @CacheStarted
+      public void cacheStarted(CacheStartedEvent event)
+      {
+         event.getCacheManager().getCache(event.getCacheName()).addListener(this);
+      }
+      
       @CacheEntryModified
-      public void nodeModified(CacheEntryModifiedEvent event)
+      public void entryModified(CacheEntryModifiedEvent event)
       {
          if (!event.isPre())
          {
-//            Fqn<String> fqn = event.getFqn();
-//            System.out.println("MyListener - Modified node " + fqn.toString());
-//            modified.add(fqn);
+            String region = event.getCache().getName();
+            System.out.println("MyListener - Modified cache entry " + region);
+            modified.add(region);
          }
       }
 
       @CacheEntryCreated
-      public void nodeCreated(CacheEntryCreatedEvent event)
-      {   
+      public void entryCreated(CacheEntryCreatedEvent event)
+      {  
          if (!event.isPre())
          {
-//            Fqn<String> fqn = event.getFqn();
-//            System.out.println("MyListener - Created node " + fqn.toString());
-//            modified.add(fqn);
+            String region = event.getCache().getName();
+            System.out.println("MyListener - Created cache entry " + region);
+            modified.add(region);
          }
-      }   
+      }
 
       @CacheEntryVisited
-      public void nodeVisited(CacheEntryVisitedEvent event)
-      {   
+      public void entryVisited(CacheEntryVisitedEvent event)
+      {  
          if (!event.isPre())
          {
-//            Fqn<String> fqn = event.getFqn();
-//            System.out.println("MyListener - Visited node " + fqn.toString());
-//            accessed.add(fqn);
+            String region = event.getCache().getName();
+            System.out.println("MyListener - Visited cache entry " + region);
+            accessed.add(region);
          }
-      }    
-      
+      }
    }
 }



More information about the jboss-cvs-commits mailing list