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.


    <cache  name="myCache"
            <searchAttribute name="online" expression="value.isOnline()" />
            <searchAttribute name="name" expression="value.getName()" />
        <cacheEventListenerFactory class="net.sf.ehcache.event.TerracottaCacheEventReplicationFactory"/>

Now in code simply register your listener with:

myCache.getCacheEventNotificationService().registerListener( myListener );

and you’ll find it will work!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s