1 Reply Latest reply on Jun 6, 2011 9:09 AM by Sanne Grinovero

    Enforce Cache Listener to be called only once in a cluster

    Madan Narra Newbie

      Hi,

       

      Registered a Listener and capturing @CacheEntryCreated events for a Cache. This application is deployed into 3 nodes which form a cluster.

       

      Added a log statement to know the key that has been added to cache. Now when an entry is added to cache, there are 3 messages printed to console for the same key on the node that inserted into cache. Below is part of log message on node3

       

      2011-06-02 14:45:18,340 INFO  [STDOUT] (ajp-10.2.84.60-8209-11) INFO : com.infinispan.test.InfinispanTestServlet - Node : node3 PUT 20:hello20
      2011-06-02 14:45:18,345 INFO  [STDOUT] (notification-thread-0) Node :: node3 Entry added with key --> 20 
      2011-06-02 14:45:18,346 INFO  [STDOUT] (notification-thread-0) Node :: node3 Entry added with key --> 20 
      2011-06-02 14:45:18,348 INFO  [STDOUT] (notification-thread-0) Node :: node3 Entry added with key --> 20 
      2011-06-02 14:45:18,399 INFO  [STDOUT] (ajp-10.2.84.60-8209-11) INFO : com.infinispan.test.InfinispanTestServlet - Node : node3 PUT 23:hello23 
      2011-06-02 14:45:18,402 INFO  [STDOUT] (notification-thread-0) Node :: node3 Entry added with key --> 23
      2011-06-02 14:45:18,403 INFO  [STDOUT] (notification-thread-0) Node :: node3 Entry added with key --> 23 
      2011-06-02 14:45:18,404 INFO  [STDOUT] (notification-thread-0) Node :: node3 Entry added with key --> 23
      

       

      This is how the Listener is configured

      @Listener(sync = false)
      public class CacheListener {
      
        @CacheEntryCreated
                public void entryAdded(CacheEntryCreatedEvent<String,String> event) {
                          if(!event.isPre())
                                    System.out.println("Node :: "+System.getProperty("server.name")+" Entry added with key --> "+event.getKey());
                }
      }
      

       

      this.cacheContainer = cacheContainer;
      numbersCache = cacheContainer.getCache("numbersCache");
      numbersCache.addListener(new CacheListener());
      

       

      What could be wrong ? Or is this the expected behaviour ? If so, is there a way to enforce that the listener gets called only on the node which has inserted entry into cache and only once.

       

      Thanks,

      Madan Narra

        • 1. Re: Enforce Cache Listener to be called only once in a cluster
          Sanne Grinovero Master

          The event is received by each node which is either a key owner of this key, or the same node in which the event was created originally.

           

          So assuming you're running the default configuration of two owners per key, you should experience the event exactly 2 or 3 times: only two in the case the originating node is also an owner.

           

          You could change your listener code to log the event only on the originating node:

           

           

          @Listener(sync = false)
          public class CacheListener {
           
            @CacheEntryCreated
                    public void entryAdded(CacheEntryCreatedEvent<String,String> event) {
                              if ( (! event.isPre()) && ( event.isOriginLocal() ) )
                                        System.out.println("Node :: "+System.getProperty("server.name")+" Entry added with key --> "+event.getKey());
                    }
          }