4 Replies Latest reply on Aug 27, 2005 8:19 AM by Bela Ban

    Can not retrieve object from nodeCreated method of listener

    canghel Newbie

      Hi,

      I am using jboss-cache version 1.2.1. I have a listener registered to a cache. When a new object is added to the cache the listener is notified with nodeCreated event. In the nodeCreated method I try to read the object from the cache, based on the key I receive, using a get call but I receive NULL.

      Is this a defect?

      I searched to see whether there is a defect reported for this and I found http://jira.jboss.com/jira/browse/JBCACHE-192

      Is there a fix or it is still an open issue?

      thanks,
      Claudiu

        • 1. Re: Can not retrieve object from nodeCreated method of liste
          Bela Ban Master

          Again, it helps if you have a unit test, take a look at TreeCacheListener test and see whether your case is covered or not. If not, I'd be happy to add another unit test so we can cover this use case.

          • 2. Re: Can not retrieve object from nodeCreated method of liste
            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(":"));
             }
             }
            }
            



            • 3. Re: Can not retrieve object from nodeCreated method of liste
              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);
               }
               }
              }
              


              Claudiu

              • 4. Re: Can not retrieve object from nodeCreated method of liste
                Bela Ban Master

                You have implemented the wrong callback: nodeCreated() doesn't give you any data, you'll need to implement nodeModified()