Showing posts with label Ehcache. Show all posts
Showing posts with label Ehcache. Show all posts

Monday, 2 December 2013

Ehcache Replication in Spring using JMS

JMS can also be used as the underlying mechanism for replication operations in Ehcache. The Ehcache jmsreplication module lets organisations with a message queue investment leverage it for caching. It provides replication between cache nodes using a replication topic, pushing of data directly to cache nodes from external topic publishers, and a JMSCacheLoader, which sends cache load requests to a queue.

Ehcache replicates using JMS as follows:

Each cache node subscribes to a predefined topic, configured as the <topicBindingName> in ehcache.xml.
Each replicated cache publishes cache Elements to that topic. Replication is configured per cache.

To set up replicated caching using JMS you need to configure a JMSCacheManagerPeerProviderFactory which is done globally for a CacheManager.

For each cache that wishing to replicate, you add a JGroupsCacheReplicatorFactory element to the cache element.

Ehcache Image

Configuration


Each cluster needs to use a fixed topic name for replication. Set up a topic using the tools in your message queue. Out of the box, both ActiveMQ and Open MQ support auto creation of destinations, so this step may be optional.

For this example, an ActiveMQ server has been used. You can download it from here.

Download and extract it and locate the bin folder. Running the batch file - activemq will start the ActiveMQ server. Its default port is 61616.



There are two things to configure for ehcache:


- The JMSCacheManagerPeerProviderFactory which is done once per CacheManager and therefore once per ehcache.xml file.

- The JMSCacheReplicatorFactory which is added to each cache's configuration if you want that cache replicated.

The main configuration happens in the JGroupsCacheManagerPeerProviderFactory connect sub-property. A connect property is passed directly to the JGroups channel and therefore all the protocol stacks and options available in JGroups can be set.
Contents of ehcache.xml:


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

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="true" monitoring="autodetect" dynamicConfig="true">
<cacheManagerPeerProviderFactory

class="net.sf.ehcache.distribution.jms.JMSCacheManagerPeerProviderFactory"

properties="initialContextFactoryName=com.spring.hibernate.activemq.MyActiveMQInitialContextFactory,
providerURL=tcp://10.35.34.193:61616, 
replicationTopicConnectionFactoryBindingName=topicConnectionFactory,
replicationTopicBindingName=ehcache, 
getQueueConnectionFactoryBindingName=queueConnectionFactory,
getQueueBindingName=ehcacheGetQueue, 
topicConnectionFactoryBindingName=topicConnectionFactory,topicBindingName=ehcache"

propertySeparator="," 

/>
<cacheManagerPeerListenerFactory
 class="org.terracotta.ehcachedx.monitor.probe.ProbePeerListenerFactory"
 properties="monitorAddress=10.35.34.193, monitorPort=9889,              memoryMeasurement=true" />

<cache name="employees" eternal="false" maxElementsInMemory="1000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="500" memoryStoreEvictionPolicy="LRU" statistics="true">

<cacheEventListenerFactory
class="net.sf.ehcache.distribution.jms.JMSCacheReplicatorFactory"
properties="replicateAsynchronously=true,
                  replicatePuts=true,
                  replicateUpdates=true,
                  replicateUpdatesViaCopy=true,
                  replicateRemovals=true,
                  asynchronousReplicationIntervalMillis=1000"
propertySeparator="," />
</cache>

</ehcache>

You need to provide your own ActiveMQInitialContextFactory for the initialContextFactoryName. An example which should work for most purposes is 

MyActiveMQInitialContextFactory.java:-

package com.spring.hibernate.activemq;


import java.net.URISyntaxException;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


import javax.naming.Context;
import javax.naming.NamingException;


import net.sf.ehcache.distribution.jms.JMSUtil;


import org.apache.activemq.jndi.ActiveMQInitialContextFactory;


public class MyActiveMQInitialContextFactory extends
ActiveMQInitialContextFactory {


/**
* Creates an initial context with {@inheritDoc}
*/
@SuppressWarnings("rawtypes")
@Override
public Context getInitialContext(Hashtable environment)
throws NamingException {


Map<String, Object> data = new ConcurrentHashMap<String, Object>();


String replicationTopicConnectionFactoryBindingName = (String) environment
.get(JMSUtil.TOPIC_CONNECTION_FACTORY_BINDING_NAME);
if (replicationTopicConnectionFactoryBindingName != null) {
try {
data.put(replicationTopicConnectionFactoryBindingName,
createConnectionFactory(environment));
} catch (URISyntaxException e) {
throw new NamingException(
"Error initialisating TopicConnectionFactory with message "
+ e.getMessage());
}
}
String getQueueConnectionfactoryBindingName = (String) environment
.get(JMSUtil.GET_QUEUE_CONNECTION_FACTORY_BINDING_NAME);


try {
data.put(getQueueConnectionfactoryBindingName,
createConnectionFactory(environment));
} catch (URISyntaxException e) {
throw new NamingException(
"Error initialisating TopicConnectionFactory with message "
+ e.getMessage());
}


String replicationTopicBindingName = (String) environment
.get(JMSUtil.REPLICATION_TOPIC_BINDING_NAME);
String getQueueBindingName = (String) environment
.get(JMSUtil.GET_QUEUE_BINDING_NAME);
if (replicationTopicBindingName != null) {
data.put(replicationTopicBindingName,
createTopic(replicationTopicBindingName));
}
data.put(getQueueBindingName, createQueue(getQueueBindingName));
return createContext(environment, data);
}
}


Note that you need to provide the as for this class as the initialContextFactoryName property for cacheManagerPeerProviderFactory

Maven Dependancies : 

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-jmsreplication</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0-b06</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>4.1.1</version>
</dependency>


NOTE:

Make sure that the firewall is turned off for the caching to work.

Only Serializable are suitable for replication.Some operations, such as remove, work off Element keys rather than the full Element itself. In this case the operation will be replicated provided the key is Serializable, even if the Element is not.


You can refer to my previous blog -
Caching in a Spring Maven Project if you have just started learning caching


You can get the complete source code from here under the SpringHibernateProjectReplicationJms

Ehcache Replication in Spring using JGroups

JGroups can be used as the underlying mechanism for the replication operations in ehcache. JGroups offers a very flexible protocol stack, reliable unicast and multicast message transmission.

On the down side JGroups can be complex to configure and some protocol stacks have dependencies on others.

To set up replicated caching using JGroups you need to configure a PeerProviderFactory of type JGroupsCacheManagerPeerProviderFactory which is done globally for a CacheManager

For each cache that will be replicated, you then need to add a cacheEventListenerFactory of type JGroupsCacheReplicatorFactory to propagate messages.


Configuration



There are two things to configure:


1. The JGroupsCacheManagerPeerProviderFactory which is done once per CacheManager and therefore once per ehcache.xml file.
2. The JGroupsCacheReplicatorFactory which is added to each cache's configuration.

The main configuration happens in the JGroupsCacheManagerPeerProviderFactory connect sub-property.

A connect property is passed directly to the JGroups channel and therefore all the protocol stacks and options available in JGroups can be set.

Suppose you have two servers in a cluster. You wish to replicated sampleCache1 and you wish to use UDP multicast as the underlying mechanism.The configuration for server1 and server2 are identical and will look like this:


<cacheManagerPeerProviderFactory

 class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"                                                properties="connect=UDP(mcast_addr=231.12.21.132;mcast_port=45566;):PINMERGE2:FD_S     OCK:VERIFY_SUSPECT:pbcast.NAKACK:UNICAST:pbcast.STABLE:FRAG:pbcast.GMS"



 propertySeparator="::"
/>


Configuring CacheReplicators



Each cache that will be replicated needs to set a cache event listener which then replicates messages to the other CacheManager peers. This is done by adding a cacheEventListenerFactory element to each cache's configuration. The properties are identical to the one used for RMI replication. The listener factory must be of typeJGroupsCacheReplicatorFactory.

For Server1 and Server2 : 

<cache name="employees" eternal="false" maxElementsInMemory="1000"
    overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" 
    timeToLiveSeconds="500" memoryStoreEvictionPolicy="LRU" statistics="true">

    <cacheEventListenerFactory
     class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
     properties="replicateAsynchronously=true, replicatePuts=true,
     replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true" />

</cache>



Sample ehcache.xml:


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

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"

updateCheck="true" monitoring="autodetect" dynamicConfig="true">
<cacheManagerPeerProviderFactory

 class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
           properties="connect=UDP(mcast_addr=231.12.21.132;mcast_port=45566;):PING:MERGE2:FD  _SOCK:VERIFY_SUSPECT:pbcast.NAKACK:UNICAST:pbcast.STABLE:FRAG:pbcast.GMS"          
 propertySeparator="::"

/>

<cacheManagerPeerListenerFactory
class="org.terracotta.ehcachedx.monitor.probe.ProbePeerListenerFactory"
properties="monitorAddress=10.35.34.193, monitorPort=9889,  memoryMeasurement=true" />

<cache name="employees" eternal="false" maxElementsInMemory="1000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="500" memoryStoreEvictionPolicy="LRU" statistics="true">

<cacheEventListenerFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true,
  replicateUpdates=true, replicateUpdatesViaCopy=true,
                replicateRemovals=true" />
 </cache>

</ehcache>


Pom.xml Dependancy:

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.6</version>
</dependency>

<dependency>
<groupId>org.jasig.ehcache</groupId>
<artifactId>ehcache-jgroups3replication</artifactId>
<version>1.7.4</version>
</dependency>


NOTE: 

Make sure that the firewall is turned off for the caching to work.

Only Serializable are suitable for replication.Some operations, such as remove, work off Element keys rather than the full Element itself. In this case the operation will be replicated provided the key is Serializable, even if the Element is not.

When configuring the CacheManagerPeerListener, note that if the value for <ipaddress of server> can be given as localhost only if all the servers are on the same machine. For servers on different machines in a network, this address should contain explicitly ip address of the machine on which the server is present.

You can refer to my previous blog -
Caching in a Spring Maven Project if you have just started learning caching


You can get the complete source code from here under the SpringHibernateProjectReplicationJGroups

Ehcache Replication in Spring using RMI

Ehcache provides replicated caching using RMI. To set up RMI replicated caching, you need to configure the CacheManager with a PeerProvider and a CacheManagerPeerListener. Then for each cache that will be replicated, you need to add one of the RMI cacheEventListener types to propagate messages. You can also optionally configure a cache to bootstrap from other caches in the cluster.


Ehcache Image

An RMI implementation is desirable because:
  • it itself is the default remoting mechanism in Java
  • it is mature
  • it allows tuning of TCP socket options
  • Element keys and values for disk storage must already be Serializable, therefore directly transmittable over RMI without the need for conversion to a third format such as XML.
  • it can be configured to pass through firewalls
  • RMI had improvements added to it with each release of Java, which can then be taken advantage of.

To set up RMI replicated caching you need to configure the CacheManager with:
1. a PeerProvider
2. a CacheManagerPeerListener

Then for each cache that will be replicated, you then need to add one of the RMI cacheEventListener types to propagate messages. You can also optionally configure a cache to bootstrap from other caches in the cluster.


Configuring the Peer Provider


Ehcache provides two mechanisms for peer discovery - manual and automatic.

This example demonstrates the automatic peer discovery.Automatic discovery uses TCP multicast to establish and maintain a multicast group. It features minimal configuration and automatic addition to and deletion of members from the group. No a priori knowledge of the servers in the cluster is required. This is recommended as the default option. Peers send heartbeats to the group once per second. If a peer has not been heard of for 5 seconds it is dropped from the group. If a new peer starts sending heartbeats it is admitted to the group.

Any cache within the configuration set up as replicated will be made available for discovery by other peers.

To set automatic peer discovery, specify the properties attribute of cacheManagerPeerProviderFactory as follows:

- peerDiscovery=automatic
- multicastGroupAddress=multicast address | multicast host   name
- multicastGroupPort=port
- timeToLive=0-255
- hostName=the hostname or IP of the interface to be used     for sending and receiving multicast packets (relevant to       mulithomed hosts only)




Suppose you have two servers in a cluster. You wish to distribute sampleCache11 and sampleCache12. The configuration required for each server is identical:

Configuration for server1 and server2: 



<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,
multicastGroupPort=4446, timeToLive=32"/>

Configuring the CacheManagerPeerListener


A CacheManagerPeerListener listens for messages from peers to the current CacheManager.

You can configure the CacheManagerPeerListener by specifiying a CacheManagerPeerListenerFactory which is used to create the CacheManagerPeerListener using the plugin mechanism.

The attributes of cacheManagerPeerListenerFactory are:

1. class - a fully qualified factory class name
2. properties - comma separated properties having meaning only to the factory.
Ehcache comes with a built-in RMI-based distribution system. The listener component is RMICacheManagerPeerListener which is configured using RMICacheManagerPeerListenerFactory. It is configured as per the following example:

For server1:

<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=<ipaddress of server1>, port=40001,
socketTimeoutMillis=2000"
socketTimeoutMillis=2000"/>

For server2:
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=<ipaddress of server2>, port=40001,
socketTimeoutMillis=2000"
socketTimeoutMillis=2000"/>

Valid properties are:

1. hostName (optional) - the hostName of the host the listener is running on. Specify where the host is multihomed and you want to control the interface over which cluster messages are received. The hostname is checked for reachability during CacheManager initialisation. If the hostName is unreachable, the CacheManager will refuse to start and an CacheException will be thrown indicating connection was refused. If unspecified, the hostname will useInetAddress.getLocalHost().getHostAddress(), which corresponds to the default host network interface. Warning: Explicitly setting this to localhost refers to the local loopback of 127.0.0.1, which is not network visible and will cause no replications to be received from remote hosts. You should only use this setting when multiple CacheManagers are on the same machine.
2. port (mandatory) - the port the listener listens on.
3. socketTimeoutMillis (optional) - the number of seconds client sockets will wait when sending messages to this listener until they give up. By default this is 2000ms.

Configuring Cache Replicators

Each cache that will be replicated needs to set a cache event listener which then replicates messages to the other CacheManager peers. This is done by adding a cacheEventListenerFactory element to each cache's configuration.

For Server1 and Server2:

<cache name="employees" eternal="false" maxElementsInMemory="1000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="500" memoryStoreEvictionPolicy="LRU" statistics="true">

<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,
replicateUpdatesViaCopy=true, replicateRemovals=true " />
</cache>



The factory recognises the following properties:

1. replicatePuts=true | false - whether new elements placed in a cache are replicated to others. Defaults to true.
2. replicateUpdates=true | false - whether new elements which override an element already existing with the same key are replicated. Defaults to true.
3. replicateRemovals=true - whether element removals are replicated. Defaults to true.
4. replicateAsynchronously=true | false - whether replications are asyncrhonous (true) or synchronous (false). Defaults to true.
5. replicateUpdatesViaCopy=true | false - whether the new elements are copied to other caches (true), or whether a remove message is sent. Defaults to true.

Sample ehcache.xml for server1: 

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="true" monitoring="autodetect" dynamicConfig="true">

<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,
multicastGroupPort=4446, timeToLive=32"/>

<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=10.35.34.193, port=40001,
socketTimeoutMillis=2000" />

<cacheManagerPeerListenerFactory
class="org.terracotta.ehcachedx.monitor.probe.ProbePeerListenerFactory"
properties="monitorAddress=10.35.34.193, monitorPort=9889,                                           memoryMeasurement=true" />

<cache name="employees" eternal="false" maxElementsInMemory="1000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="500" memoryStoreEvictionPolicy="LRU" statistics="true">

<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true,                                           replicateUpdates=true, replicateUpdatesViaCopy=true, 
                replicateRemovals=true " />
</cache>
</ehcache>

Pom.xml dependancy:

 <dependency>

<groupId>net.sf.ehcache</groupId>

<artifactId>ehcache-core</artifactId>

<version>2.6.6</version>

 </dependency>

NOTE: 

Make sure that the firewall is turned off for the caching to work. 

Only Serializable are suitable for replication.Some operations, such as remove, work off Element keys rather than the full Element itself. In this case the operation will be replicated provided the key is Serializable, even if the Element is not.

When configuring the CacheManagerPeerListener, note that if the value for <ipaddress of server> can be given as localhost only if all the servers are on the same machine. For servers on different machines in a network, this address should contain explicitly ip address of the machine on which the server is present.

The automatic peer discovery process relies on multicast. Multicast can be blocked by routers. Virtualisation technologies like Xen and VMWare may be blocking multicast. If so enable it. You may also need to turn it on in the configuration for your network interface card. An easy way to tell if your multicast is getting through is to use the Ehcache remote debugger and watch for the heartbeat packets to arrive.

You can refer to my previous blog -
Caching in a Spring Maven Project if you have just started learning caching

You can get the complete source code from here under the SpringHibernateProject

Thursday, 21 November 2013

Caching in Spring using Ehcache and monitoring it using the Ehcache Monitor

Cache as we all know is a component that transparently stores data so that the future requests for that data can be served faster.Previously calculated or duplicates of the original data are stored in the cache.

Ehcache is java's most widely used cache. It is an open source, standards-based cache for boosting performance, offloading your database, and simplifying scalability. It's the most widely-used Java-based cache because it's robust, proven, and full-featured. Ehcache scales from in-process, with one or more nodes, all the way to mixed in-process/out-of-process configurations with terabyte-sized caches.

We can cache the values returned by any function based on the arguments that are passed to that function.Cache values are stored in the form of key-value pairs.The function arguments act as the key. One can specify which argument/arguments act as the key.Some easy configurations are required to enable caching in your application.

The Ehcache Monitor is an add-on tool for Ehcache which provides enterprise-class monitoring and management capabilities for use in both development and production. It is intended to help understand and tune cache usage, detect errors, and provide an easy to use access point to integrate with production management systems. It also provides administrative functionality such as the ability to forcefully remove items from caches.

To install the Monitor, you need add the Monitor Probe JAR to your app and a few lines of config in ehcache.xml. The package contains a probe and a server. The probe installs with your existing Ehcache cache instance, and communicates to a central server. The server aggregates data from multiple probes. It can be accessed via a simple web UI, as well as a scriptable API.



Steps:

1.Download ehcache core jars from here or  if you are using Maven, add the following dependency into your pom.xml:

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.6</version>
</dependency>

2. Download the Ehcache monitor kit from here . After you get the Ehcache monitor kit look for the ehcache-probe-ver.jar, copy this jar and paste it into your WEB-INF/lib folder. 

3. Ehcache.xml is the configuration file having all the necessary configuration. Create this xml file and paste it into your classpath (src/main/resources).Its contents are as follows:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="true" monitoring="autodetect" dynamicConfig="true">

<cacheManagerPeerListenerFactory
     class="org.terracotta.ehcachedx.monitor.probe.ProbePeerListenerFactory"
     properties="monitorAddress=10.35.34.193, monitorPort=9889,        memoryMeasurement=true" />

<cache name="employees" eternal="false" maxElementsInMemory="1000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="500" memoryStoreEvictionPolicy="LRU" statistics="true">
</cache>

</ehcache>

The cacheManagerPeerListenerFactory has to be configured with details about where the Ehcache Monitor server is listening. Monitor Address is the ip and monitorPort is the port at which the monitor server is listening. The MemoryMeasurement Attribute will specify whether the Memory Measurement is to be done.

All the properties about every cache are also to be mentioned. 
  • timeToLive - The maximum number of seconds an element can exist in the cache regardless of use. The element expires at this limit and will no longer be returned from the cache. The default value is 0, which means no TTL eviction takes place (infinite lifetime).
  • timeToIdle - The maximum number of seconds an element can exist in the cache without being accessed. The element expires at this limit and will no longer be returned from the cache. The default value is 0, which means no TTI eviction takes place (infinite lifetime).
  • Local sizing attributes maxEntriesLocalHeap, maxBytesLocalHeap, maxEntriesLocalDisk, maxBytesLocalDisk.
  • Note that the eternal attribute, when set to "true", overrides timeToLive and timeToIdle so that no expiration can take place.

4. Define the Cache Manager bean in your spring-servlet.xml
and enable the spring cache annotations:


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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-3.1.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

        http://www.springframework.org/schema/cache 

        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:component-scan base-package="com.spring.hibernate" />

<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"  >
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"       />
</list>
</property>
</bean>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://10.35.34.193:3306/test11" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration
</value>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<cache:annotation-driven />
<bean id="cacheManager"
  class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehcache" />

<bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml" p:shared="true" />

</beans>

5. Use the Spring Caching Annotations in your code:

Ex-

@Cacheable("employees")

public Employee getEmployee(int empId) {

   Employee emp = employeeDao.getEmployee(empId);

   return emp;

}



@CacheEvict (value = "employees", key="#empId")
public void deleteEmployee(int empId) {
    employeeDao.deleteEmployee(empId);
}



@Cacheable is used with the function whose return values are to be cached. Cache values are stored in the form of key-value pairs. In this case, empId is the key. "employees" is the name of the cache where these values are stored.



@CacheEvict is used to evict the entries from your cache. An optional key parameter can be given to specify which of the arguments are used as keys.


Start the Ehcache Monitor:


Include SLF4J logging jars.Ehcache 1.7.1 and above require SLF4J.Earlier versions used commons logging. The probe, like all new Ehcache modules, uses SLF4J, which is becoming a new standard in open source projects.

If you are using Ehcache 1.5 to 1.7.0, you will need to add slf4j-api and one concrete logger. If you are using Ehcache 1.7.1 and above you should not need to do anything because you will already be using slf4j-api and one concrete logger.

To start the monitor, run the startup script provided in the bin directory: startup.sh on Unix and startup.bat on Microsoft Windows.

NOTE: If errors occur at startup, remove the line -j "$PRGDIR/etc/jetty.xml" \ (or -j %PRGDIR%\etc\jetty.xml ^) from the startup script.

The monitor port selected in this script should match the port specified in ehcache.xml. The monitor can be configured, including interface, port and simple security settings, in etc/ehcache-monitor.conf. Note that for the commercial version, the location of your license file must be specified in ehcache-monitor.conf.


Example:
license_file=/Users/karthik/Documents/workspace/lib/license/terracotta-license.key 
The monitor connection timeout can also be configured.The web-based GUI is available by pointing your browser at http://:/monitor. For a default installation on the local machine, this would be http://localhost:9889/monitor

NOTE:

Note that Caching will not work on private methods of a class. Make sure Cacheable method is public. Caching will not work if you try to call the Cacheable method from the same class in which it has been written.



You can download the Sample source code from here

under SpringHibernateProjectCaching



References: