EHCACHE Replication with JGROUP in Spring

Introduction:

In context of multiple application server, you need to replicate ehcache among them for consistency, we have option of using JGroup for this purpose.

Consider maven-spring web application.

You have to specify serveral dependancies in pom.xml including Spring and ehcache.

Sample working pom.xml file is as follows.

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.arceler</groupId>
<artifactId>ehcache</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>ehcache spring annotations demo</name>
<url>http://maven.apache.org</url>
<dependencies>

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>1.5.0</version>
</dependency>

<dependency>
<groupId>jgroups</groupId>
<artifactId>jgroups-all</artifactId>
<version>2.2.5</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope></dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.3.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>3.0.3.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.googlecode.ehcache-spring-annotations</groupId>
<artifactId>ehcache-spring-annotations</artifactId>
<version>1.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.5</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.0.3.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<finalName>ehcache-jgroups</finalName>
</build>
</project>

Now create a bean in Application context.

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation"  value="/WEB-INF/ehcache.xml"/>
</bean>

To set up replicated caching using JGroups you need to configure a PeerProviderFactory of type JGroupsCacheManagerPeerProviderFactory.

Complete Configuration is as follows.

ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.sf.net/ehcache.xsd">

<diskStore path="java.io.tmpdir" />

<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
properties="connect=TCP(start_port=49000): TCPPING(initial_hosts=10.35.34.229[49000];port_range=10;timeout=3000;
num_initial_members=3;up_thread=true;down_thread=true): VERIFY_SUSPECT(timeout=1500;down_thread=false;up_thread=false):
pbcast.NAKACK(down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):
pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false; print_local_addr=false;down_thread=true;up_thread=true)"
propertySeparator="::" />

<defaultCache eternal="true" maxElementsInMemory="100"
overflowToDisk="false" />
<cache name="messageCache" maxElementsInMemory="10000"
maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"
diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true" />
</cache>

</ehcache>

Description:

a)Configuring cacheManagerPeerProviderFactory

  • connect=TCP(start_port=49000):TCPPING(initial_hosts=10.35.34.229[49000];

Here you have to Specify port number for start_port and Ip address for initial_hosts.

You can specify multiple Ip addresses of appliation servers.

  • port_range=10;

Port range means TCP pings each port from start_port to (start_port + port_range – 1)

b)Configuring CacheReplicators

The configuration options are explained below:

class – use net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory

The factory recognises the following properties:

  • replicatePuts=true | false – whether new elements placed in a cache are replicated to others. Defaults to true.
  • replicateUpdates=true | false – whether new elements which override an element already existing with the same key are replicated. Defaults to true.
  • replicateRemovals=true – whether element removals are replicated. Defaults to true.
  • replicateAsynchronously=true | false – whether replications are asyncrhonous (true) or synchronous (false). Defaults to true.
  • replicateUpdatesViaCopy=true | false – whether the new elements are copied to other caches (true), or whether a remove message is sent. Defaults to true.
  • asynchronousReplicationIntervalMillis default 1000ms Time between updates when replication is asynchroneous
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