3 Replies Latest reply on Jan 3, 2011 9:58 AM by Vladimir Blagojevic

    L1 Cache & Cache EventListeners

    canokar Newbie



      i ask myself if entries which are removed from L1 cache also notify the registered listeners?

      I got a distributed Cache with one entry loaded from Persistent FileCacheStore. if i connect with

      a second Cache in ClusteredMode (CacheMode Dist_Sync) the Lifespan of the L1 Cache ist set to

      500ms. I didn't get @CacheEntryCreated nor @CacheEntryRemoved Events. Maybe i have a wrong

      understanding of the EventListeners, it seems that they did'nt work for L1 Cache.

        • 1. Re: L1 Cache & Cache EventListeners
          Vladimir Blagojevic Master

          Hi Cano,


          I believe that even though an entry is in L1 you should get notified about its eviction. However, you would have to listen for either CacheEntryEvicted and CacheEntryRemoved events. How are you attaching these listeners? Post your code snippet.




          • 2. Re: L1 Cache & Cache EventListeners
            canokar Newbie

            Hi Vladimir,


            here is my test code. On the opposite i create a Cache and load the content of the cache via FileStoreConfig.

            Currently there are only one element inside the cache (Key=key and Value=value) for testing purpose.

            The JUnit Test successfully connects to the "Server" Cache and gets the correct value for Key=key.


            import org.infinispan.Cache;
            import org.infinispan.config.CacheLoaderManagerConfig;
            import org.infinispan.config.Configuration;
            import org.infinispan.config.GlobalConfiguration;
            import org.infinispan.config.Configuration.CacheMode;
            import org.infinispan.eviction.EvictionStrategy;
            import org.infinispan.loaders.CacheStoreConfig;
            import org.infinispan.loaders.file.FileCacheStoreConfig;
            import org.infinispan.manager.DefaultCacheManager;
            import org.infinispan.manager.EmbeddedCacheManager;
            import org.infinispan.notifications.Listener;
            import org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated;
            import org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved;
            import org.infinispan.notifications.cachelistener.event.CacheEntryCreatedEvent;
            import org.infinispan.notifications.cachelistener.event.CacheEntryRemovedEvent;
            import org.junit.Before;
            import org.junit.Test;


            public class CacheTest {


              public void setUp() throws Exception {


              public void accessCache() {
                EmbeddedCacheManager manager = getCacheManager();
                Cache<String, String> cache = manager.getCache("CacheStore");
                cache.addListener(new DummyListener());


                System.out.println("Cache value for \"key\" = " + cache.get("key"));
                try {
                } catch (InterruptedException e) {
                System.out.println("Cache value for \"key\" = " + cache.get("key"));
                try {
                } catch (InterruptedException e) {
                System.out.println("Cache value for \"key\" = " + cache.get("key"));




              private EmbeddedCacheManager getCacheManager() {
                GlobalConfiguration globalConf = GlobalConfiguration.getClusteredDefault();
                Configuration config = new Configuration();
                config.setCacheLoaderManagerConfig(new CacheLoaderManagerConfig());
                return new DefaultCacheManager(globalConf, config);
              public class DummyListener {
                public void print(CacheEntryCreatedEvent<String, String> event) {
                  System.out.println("New entry " + event.getKey() + " created in the cache");
                public void print(CacheEntryRemovedEvent<String, String> event) {
                  System.out.println("Entry " + event.getKey() + " removed in the cache");

            • 3. Re: L1 Cache & Cache EventListeners
              Vladimir Blagojevic Master

              The key/value pair was either transfered during state transfer or there was a remote get. That is why you are not seeing listeners invoked. Do a put or remove in this test code and observe if the listener is getting invoked.