Distributed Ehcache – RMI replication (configuration)

I am having some fun with Ehcache esp. in distributed caching. Distributed caching is really important in a clustered environment. Read more about distributed caching here – http://ehcache.org/documentation/distributed_caching.html.

I have downloaded Ehcache 2.1 from http://sourceforge.net/projects/ehcache/files/ (ehcache-2.1.0-distribution.tar.gz, 48 mb approx.) which contains Ehcache core and the Terracotta libraries. Once if we have these libraries, we can configure the caches, peers and listeners. I have created a basic cache – MyCache – and my configuration file looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true" monitoring="autodetect"
         dynamicConfig="true" >

    <diskStore path="java.io.tmpdir"/>
    <cacheManagerEventListenerFactory class="" properties=""/>

    <defaultCache
           maxElementsInMemory="0"
           eternal="false"
           timeToIdleSeconds="1200"
           timeToLiveSeconds="1200">
    </defaultCache>

        <!-- For RMI replication (Setting the peer provider factory) -->
	<cacheManagerPeerProviderFactory
		class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
		properties="peerDiscovery=manual, rmiUrls=//remoteHostIpAddress:40000/MyCache"
		propertySeparator="," />

	<!-- For RMI replication (Setting the peer listener factory) -->
	<cacheManagerPeerListenerFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
            properties="hostName=localMachineIpAddress, port=40000, socketTimeoutMillis=120000"
            propertySeparator=","/>

	<cache name="MyCache"
		maxElementsInMemory="1000"
		eternal="false"
		overflowToDisk="true"
		diskSpoolBufferSizeMB="20"
		timeToLiveSeconds="3000"
		timeToIdleSeconds="3000"
		memoryStoreEvictionPolicy="LFU">

		<!-- Cache event listener -->
		<cacheEventListenerFactory
			class="subin.cache.listener.factory.CacheEventListenerFactoryImpl"
			properties="" />

		<!-- RMI replication listener -->
		<cacheEventListenerFactory
        	class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
        	properties="replicateAsynchronously=true,
		         replicatePuts=true,
		         replicatePutsViaCopy=true,
		         replicateUpdates=true,
		         replicateUpdatesViaCopy=true,
		         replicateRemovals=true" />

		<!-- RMI Cache bootstrap -->
		<bootstrapCacheLoaderFactory
			class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
			properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000"
			propertySeparator="," />

	</cache>

</ehcache>

Initializing the CacheManager & Accessing the cache

// Testing the cache creation in ver 2.1
CacheManager manager = new CacheManager("/path/to/config/file.xml");

// Get the instance of "MyCache"
Cache myCache = manager.getCache("MyCache");

// Add an element to "MyCache". Its preferred to have both key and value
// serializable
Element aCacheElement = new Element(key, value);
myCache.put(aCacheElement);

Useful Links

RMI replication: http://ehcache.org/documentation/distributed_caching_with_rmi.html
Configuration: http://ehcache.org/documentation/configuration.html

Advertisements