1 Reply Latest reply on Apr 24, 2013 9:58 AM by Anna Manukyan

    Infinispan Listener EntryRemovedEvent

    R S Newbie



      is it possible that entries are removed from infinispan 5.1.8 used as Hibernate 4.2.0 2LC without notifying the listener? That is EntryRemovedEvent is working not in all cases.


      entityManager.find(...) //entity is in cache

      entityManager.getEntityManagerFactory().getCache.contains(...) == true //ok, it is realy in cache

      //here doing some other magic stuff....

      //here I expect to be notified about EntryRemovedEvent....

      entityManager.getEntityManagerFactory().getCache.contains(...) == false //entity is not anymore in cache


      1) What kind of "magic" should happen that Listener is not notified about  EntryRemovedEvent?

      2) Is entityManager.getEntityManagerFactory().getCache right method to access the 2LC?

        • 1. Re: Infinispan Listener EntryRemovedEvent
          Anna Manukyan Newbie



          please find the answers to your questions below:


          1. The correct way to get the cache which is the one which is used as a region for your specific entity is:


          EntityManagerFactoryImpl entityManagerFactoryImpl = (EntityManagerFactoryImpl) entityManager.getEntityManagerFactory();
          InfinispanRegionFactory factory = (InfinispanRegionFactory) entityManagerFactoryImpl.getSessionFactory().getSettings().getRegionFactory();
          EmbeddedCacheManager cacheManager = factory.getCacheManager();  //<-- this way you are accessing the cache manager;

          From this point, the first that you can do is to list the available cache names with method cacheManager.getCacheNames() to see what caches do you have:


          Set<String> cacheNames = cacheManager.getCacheNames();


             2. Make sure that your entity class is annotated with following annotations:


          @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL, region = "cachename")
          • Make sure that the CacheConcurrencyStrategy is set to TRANSACTIONAL, as in case of READ_ONLY no of other operations rather than read would work.
          • Although you have specified in hibernate.cfg.xml that entities should be placed in specific cache, but anyway if you will not tell @Cache annotation to use your specific region, it will create subregion - the name would be the fully qualified name of your entity class. So the region attribute in code above is necessary if you want to add listener to it.


             3. After these steps, create a Listener (see sample listener code below):



             public class NotifListener {
                public void handleCacheRemove(CacheEntryRemovedEvent event) {
                   System.out.println("remove Started"); 

             4. Register your listener on your cache, before making operations necessary for you:



          cacheManager.getCache("cachename").addListener(new NotifListener());


          Important: Please note, that the all (almost all) events are called twice - before operation is executed and after operation is executed.

          If you need to process it once, then the event object passed as a parameter to the handleCacheRemove(...) method, has method isPre() which returns true, if the call is before operation execution.


          The instructions above refer to all events.


          Hope this will help.


          Best regards,