Broadcasting Ehcache CacheEvent’s across a Terracotta cluster

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!

Why is ehcache on Terracotta using Hibernate when it’s not meant to be?

I have this weird and interesting problem with hibernate interfering with ehcache on Terracotta.

I have 2 clients, one jboss the other a standalone vm. Both share a common ehcache instance on Terracotta.

Now this works, depending on which client starts first after terracotta.

If the standalone vm starts first then everything is fine.

If jboss starts first then when the standalone vm tries to use the cache it fails because the cache has configured itself to use HibernateElementSerializationStrategy for the keys. As this has a classpath dependency on org.hibernate.cache.CacheKey and that is not in the standalone vm the cache blows up.

So, is there a way to configure ehcache to use a specific strategy and not hibernate? For me hibernate is legacy so ehcache should have no need to refer to it.

%d bloggers like this: