-
1. Re: Infinispan Listener EntryRemovedEvent
amanukyan Apr 24, 2013 9:58 AM (in response to rsmrsmrsm)Hi,
please find the answers to your questions below:
- 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:
@Entity @Cacheable @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):
@Listener(sync=true) public class NotifListener { @CacheEntryRemoved 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()); entityManager.remove(entityObj);
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,
Anna.