Monday 14 January 2013

Ehcache Monitoring

Ehcache monitor allows you to monitor and manage the cached entries in your application cache
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 provides administrative functionality such as the ability to forcefully remove items from caches..It is add-on by ehcache ,it can be easily added into working ehcache project.For developemt purpose it is free but for production in is commercially lichened.
Steps to download and install ehcache monitoring:
1. Download ehcache monitor from Terracotta's site form here.
 It is not free for production but for developement you can download trial version.
2.Unzip the package.It has following structure.Bin directory has binary files for startup and shutdown of ehcache monitor.Etc director has configuration related file where you can configure server and all other thing.ehcache monitor uses inbuild jetty server.Lib folder has required jar.


3.You need to add ehcache probe jar to your application classpath to register probe with ehcache monitor.Probe jar is present in lib directory.If your using maven you first need to register the repository of ehcache probe with  and then add ehcache probe jar dependency in pom.xml.

 mvn install:install-file -Dfile=lib/ehcache-probe-<version>.jar \
                             -Dpackaging=jar \
                             -DgroupId=org.terracotta \
                             -DartifactId=ehcache-probe \
                             -Dversion=<version>
above command will register the repository and then you can add dependency to you application as follow:
<dependency>
  <groupId>org.terracotta</groupId>
  <artifactId>ehcache-probe</artifactId>
  <version>${ehcache.probe}</version>
</dependency>   
 or
You can add following to your pom.xml

<repository>
  <id>terracotta-releases</id>
  <url>http://www.terracotta.org/download/reflector/releases</url>
</repository>
<dependency>
  <groupId>org.terracotta</groupId>
  <artifactId>ehcache-probe</artifactId>
  <version>[version]</version>
</dependency>

4.Add echcache probe listener to your ehcache.xml file.E.g.

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
 dynamicConfig="true" monitoring="autodetect">
  
    <!-- Location of persistent caches on disk -->
    <diskStore path="java.io.tmpdir/MxlServiceLayer" />
    <cacheManagerPeerListenerFactory
        class="org.terracotta.ehcachedx.monitor.probe.ProbePeerListenerFactory"
        properties="monitorAddress=localhost, monitorPort=9889, memoryMeasurement=true" />
        <defaultCache eternal="false" maxElementsInMemory="1000"
        overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
        timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" statistics="true"/>
       <cache name="authToken" eternal="false"
        maxElementsInMemory="100" overflowToDisk="false" diskPersistent="true"
        timeToIdleSeconds="0" timeToLiveSeconds="31536000"
        memoryStoreEvictionPolicy="LRU"  statistics="true"/>
       </ehcache>

You need to change monitor address and port as per the configuration's made for ehcache monitor.By default it uses 9889 port but ,It can be configured by modifying  ehcache-monitor.conf file.If schema for probe listener is not found then you may need to change xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" to xsi:noNamespaceSchemaLocation="ehcache.xsd".

5. Create the bean of cache manager in appcontest.xml with ehcache.xml is classpath as follow.
<ehcache:annotation-driven />
   
    <ehcache:config cache-manager="cacheManager">
        <ehcache:evict-expired-elements
            interval="60" />
    </ehcache:config>
   
    <bean id="cacheManager"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    </bean>
 6.Now run the application and you can see cacheManager register to cache Monitor at
http://localhost:9889/monitor/
E.G.

Ehcache Monitor UI

 7.If  you run your application on tomcat from eclipse you may get ClassNotFoundExecption for
org.terracotta.ehcachedx.monitor.probe.ProbePeerListenerFactory class even if you have ehcache-probe-<version>.jar file in your classpath you should make sure that your project's deployment assets should have maven dependencies.
8.That it. 

References :

http://ehcache.org/documentation/operations/monitor



No comments:

Post a Comment