I’ve been trying to get CacheEvent’s working with ehcache – specifically across a terracotta cluster so that if an element is added, removed or updated then every node in the cluster is notified.
Now going by the api, this should be a simple case of creating a CacheEventListener and attaching it to the individual Ehcache. It’s not that simple. Doing that only means that events occuring on the local instance get the events – the other nodes don’t get them.
Now it’s not easy to find but the solution is to actually add a specific CacheEventListenerFactory to the cache.
I.e.
<cache name="myCache" maxElementsInMemory="100000" overflowToDisk="false" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="3600" memoryStoreEvictionPolicy="LFU"> <searchable> <searchAttribute name="online" expression="value.isOnline()" /> <searchAttribute name="name" expression="value.getName()" /> </searchable> <terracotta/> <cacheEventListenerFactory class="net.sf.ehcache.event.TerracottaCacheEventReplicationFactory"/> </cache>
Now in code simply register your listener with:
myCache.getCacheEventNotificationService().registerListener( myListener );
and you’ll find it will work!