In this post I will show how to register Infinispan listeners in Spring when Infinispan is used as second level cache (2LC) for Hibernate. So let’s start:
- Annotate your entity classes as cacheable. Pay attention to the cache region name ("cache_name") specified in the region attribute of Cache annotation. @Entity @Cache(region = "cache_name", usage = CacheConcurrencyStrategy.TRANSACTIONAL) @Cacheable @Table(name = "ENTITY_TABLE") public class MyEntity implements Serializable { // entity fields }
- Write your custom InfinispanRegionFactory as it is shown below. It extends from Hibernate InfinispanRegionFactory and its logic is very simple. The only thing which it does is to register listeners after its start. Pay attention to the named cache ("cache_name") to which listeners are registered. It should be the same as the cache region name used by your entities.