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!

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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