5 Replies Latest reply on Aug 29, 2005 3:45 AM by Bela Ban

    Listener is not notified with nodeRemoved

    canghel Newbie

      Hi,

      I am using jboss cache version 1.2.1 and the listeners I register to caches are not notified with nodeRemoved when a node is removed from cache. I searched to see whether there is a bug reported for this and I found [http://jira.jboss.com/jira/browse/JBCACHE-119]. The bug is reported as fixed in 1.2.3. I took the jboss-cache.jar from 1.2.3 distribution but I still have the problem.

      Is this still a problem in jboss-cache? Is there some newer fix in some newer version?

      thanks,
      Claudiu
      [/url]

        • 1. Re: Listener is not notified with nodeRemoved
          Bela Ban Master

          Can you provide a unit test ? TreeCacheListenerTest tests this, and we *do* get the notification. Can you take a look at TreeCacheListenerTest ?

          • 2. Re: Listener is not notified with nodeRemoved
            canghel Newbie

            I did not have the chance to look over the TreeCacheListener test but below I put the source of the unit test which fails with the problems I reported:

            import junit.framework.TestCase;
            import org.jboss.cache.TreeCache;
            import org.jboss.cache.TreeCacheListener;
            import org.jboss.cache.Fqn;
            import org.jboss.cache.CacheException;
            import org.jgroups.View;
            
            import java.util.List;
            import java.util.ArrayList;
            
            /**
             * Responsible to test the {@link TreeCache} outside AS.
             *
             * @author Claudiu Anghel
             * @version 1.0
             */
            public class TreeCacheListenerTest extends TestCase {
             private TreeCache treeCache;
            
             protected void setUp() throws Exception {
             super.setUp();
             treeCache = new UnitTestableTreeCache();
             }
            
             public void testNodeCreated() throws Exception {
             SimpleTreeCacheListener listener = new SimpleTreeCacheListener(treeCache);
             treeCache.addTreeCacheListener(listener);
             String key = String.valueOf(System.currentTimeMillis());
             treeCache.put("/UT/TestObjects/String:" + key, key, "TestValue");
             assertNotNull("The added object could not be retrieved by cache listener upon nodeCreated event.",
             listener.getObjRetrievedByNodeCreated());
             }
            
             public void testNodeRemoved() throws Exception {
             SimpleTreeCacheListener listener = new SimpleTreeCacheListener(treeCache);
             treeCache.addTreeCacheListener(listener);
             String key = String.valueOf(System.currentTimeMillis());
             String fqn = "/UT/TestObjects/String:" + key;
             treeCache.put(fqn, key, "TestValue");
             treeCache.remove(fqn, key);
             List removedNodeFqns = listener.getRemovedNodeFqns();
             assertEquals("Wrong number of nodes are reported as removed.", 1, removedNodeFqns.size());
             }
            
             private static class UnitTestableTreeCache extends TreeCache {
             public UnitTestableTreeCache() throws Exception {
             createInterceptorChain();
             }
             }
            
             private static class SimpleTreeCacheListener implements TreeCacheListener {
             private TreeCache treeCache;
             private Object objRetrievedByNodeCreated;
             private List removedNodeFqns = new ArrayList();
            
             public SimpleTreeCacheListener(TreeCache treeCache) {
             this.treeCache = treeCache;
             }
            
             public void nodeCreated(Fqn fqn) {
             try {
             if (fqn.size() < 3) {
             return;
             }
             objRetrievedByNodeCreated = treeCache.get(fqn, getKey(fqn));
             }
             catch (CacheException e) {
             e.printStackTrace();
             }
             }
            
             public void nodeRemoved(Fqn fqn) {
             if (fqn.size() < 3) {
             return;
             }
             removedNodeFqns.add(fqn);
             }
            
             public void nodeLoaded(Fqn fqn) {
             /* do nothing */
             }
            
             public void nodeEvicted(Fqn fqn) {
             /* do nothing */
             }
            
             public void nodeModified(Fqn fqn) {
             /* do nothing */
             }
            
             public void nodeVisited(Fqn fqn) {
             /* do nothing */
             }
            
             public void cacheStarted(TreeCache cache) {
             /* do nothing */
             }
            
             public void cacheStopped(TreeCache cache) {
             /* do nothing */
             }
            
             public void viewChange(View new_view) // might be MergeView after merging
             {
             /* do nothing */
             }
            
             public Object getObjRetrievedByNodeCreated() {
             return objRetrievedByNodeCreated;
             }
            
             public List getRemovedNodeFqns() {
             return removedNodeFqns;
             }
            
             private String getKey(Fqn fqn) {
             String lastPart = (String) fqn.get(2);
             return lastPart.substring(lastPart.indexOf(":"));
             }
             }
            }
            


            I hope you can give me more details based on this unit test.

            thanks,
            Claudiu

            • 3. Re: Listener is not notified with nodeRemoved
              canghel Newbie

              I had a small bug in the UT; I fixed it and the UT still reproduces the problems:

              import junit.framework.TestCase;
              import org.jboss.cache.TreeCache;
              import org.jboss.cache.TreeCacheListener;
              import org.jboss.cache.Fqn;
              import org.jboss.cache.CacheException;
              import org.jgroups.View;
              
              import java.util.List;
              import java.util.ArrayList;
              
              /**
               * Responsible to test the {@link TreeCache} outside AS.
               *
               * @author Claudiu Anghel
               * @version 1.0
               */
              public class TreeCacheListenerTest extends TestCase {
               private TreeCache treeCache;
              
               protected void setUp() throws Exception {
               super.setUp();
               treeCache = new UnitTestableTreeCache();
               }
              
               public void testNodeCreated() throws Exception {
               SimpleTreeCacheListener listener = new SimpleTreeCacheListener(treeCache);
               treeCache.addTreeCacheListener(listener);
               String key = String.valueOf(System.currentTimeMillis());
               treeCache.put("/UT/TestObjects/String:" + key, key, "TestValue");
               assertNotNull("The added object could not be retrieved by cache listener upon nodeCreated event.",
               listener.getObjRetrievedByNodeCreated());
               }
              
               public void testNodeRemoved() throws Exception {
               SimpleTreeCacheListener listener = new SimpleTreeCacheListener(treeCache);
               treeCache.addTreeCacheListener(listener);
               String key = String.valueOf(System.currentTimeMillis());
               String fqn = "/UT/TestObjects/String:" + key;
               treeCache.put(fqn, key, "TestValue");
               treeCache.remove(fqn, key);
               List removedNodeFqns = listener.getRemovedNodeFqns();
               assertEquals("Wrong number of nodes are reported as removed.", 1, removedNodeFqns.size());
               }
              
               private static class UnitTestableTreeCache extends TreeCache {
               public UnitTestableTreeCache() throws Exception {
               createInterceptorChain();
               }
               }
              
               private static class SimpleTreeCacheListener implements TreeCacheListener {
               private TreeCache treeCache;
               private Object objRetrievedByNodeCreated;
               private List removedNodeFqns = new ArrayList();
              
               public SimpleTreeCacheListener(TreeCache treeCache) {
               this.treeCache = treeCache;
               }
              
               public void nodeCreated(Fqn fqn) {
               try {
               if (fqn.size() < 3) {
               return;
               }
               objRetrievedByNodeCreated = treeCache.get(fqn, getKey(fqn));
               }
               catch (CacheException e) {
               e.printStackTrace();
               }
               }
              
               public void nodeRemoved(Fqn fqn) {
               if (fqn.size() < 3) {
               return;
               }
               removedNodeFqns.add(fqn);
               }
              
               public void nodeLoaded(Fqn fqn) {
               /* do nothing */
               }
              
               public void nodeEvicted(Fqn fqn) {
               /* do nothing */
               }
              
               public void nodeModified(Fqn fqn) {
               /* do nothing */
               }
              
               public void nodeVisited(Fqn fqn) {
               /* do nothing */
               }
              
               public void cacheStarted(TreeCache cache) {
               /* do nothing */
               }
              
               public void cacheStopped(TreeCache cache) {
               /* do nothing */
               }
              
               public void viewChange(View new_view) // might be MergeView after merging
               {
               /* do nothing */
               }
              
               public Object getObjRetrievedByNodeCreated() {
               return objRetrievedByNodeCreated;
               }
              
               public List getRemovedNodeFqns() {
               return removedNodeFqns;
               }
              
               private String getKey(Fqn fqn) {
               String lastPart = (String) fqn.get(2);
               return lastPart.substring(lastPart.indexOf(":") + 1);
               }
               }
              }
              


              thanks,
              Claudiu

              • 4. Re: Listener is not notified with nodeRemoved
                canghel Newbie

                Hi,

                I think in case of remove(String fqn, Object key) only nodeModified is called; nodeRemoved is called only for remove(Fqn fqn). Initially I thought that for all remove methods nodeRemoved should be called but it makes sense that nodeRemoved is called only in case of remove(Fqn fqn).

                Please correct me if I am wrong.

                thanks,
                Claudiu

                • 5. Re: Listener is not notified with nodeRemoved
                  Bela Ban Master

                  Yes, those are the semantics