9 Replies Latest reply on Jun 12, 2012 10:52 AM by galder.zamarreno

    Infinispan Cache Event listener - CacheEntryCreated / Modified / Removed

    ilangchezhiyan

      I have started working to integrate Infinispan cache mechanism in my application.

       

      We plan to deploy our application in a clustered mode for scalability. When operated in this mode, some user specific session data will be available with any of the nodes or almost all the nodes. Whenever user logs out, session data is cleaned up in the node which processes the logout call but the same data is available with other nodes of the cluster and requires cleanup to ensure we have consistent data across all the nodes.

       

      The initial plan was to store the session data in to infinispan cache, but we rely on a third party server which enforces us to store some un-serializable data in to the cache. Due to this I am not able to use Infinispan cache as such.

       

      So my approach was to use Event Listeners with Cache Manager and implement my session cleanup logic in the CacheEntryCreated event. I am using the below sample code

       

      public static void main(String[] args){

               

              //code to initialize and add listener to cache manager

      CacheManager cm = new DefaultCacheManager("conf/cluster-config.xml");
            cm.addListener(new SampleListner());
            cm.start();
            cache = cm.getCache();

      CacheManager cm = new DefaultCacheManager("conf/cluster-config.xml");

      cm.addListener(new SampleListner());

      cm.start();

      cache = cm.getCache();

      }

       

      // event listener class

      @Listener

      public class SampleListner {


        @CacheStarted

        public void handleStart(Event event) {

          System.out.println("Cache Started... ");

        }


        @CacheStopped

        public void handleStop(Event event) {

          System.out.println("Cache shudown.... ");

        }


        @CacheEntryCreated

        public void cacheEntryCreated(CacheEntryCreatedEvent e) {

          System.out.println("Added a entry to cache...");

          // ---------SESSION CLEANUP CODE TO BE ADDED----------------------------

        }

      }

       

      Using the above code, I am able to successfully start two Cache instances in two different machines and share data between them. Both handleStart() and handleStop() methods are triggered appropriately.

       

       

      But when I add data to the cache using cache.put(key,value), the cacheEntryCreated() method is not triggered in both local as well as other nodes of the cluster.

       

      Any thoughts/comments on my approach..?

       

      I also want to know if CacheEntryCreated() will be triggered for the regular cache.put(key, value) method calls in both local as well as other nodes of the cluster.....

       

      I stuck up at this point and any comments would be really appreciated.