Tag Archives: ehcache

Ehcache statistics

Here is what you need to do if you want to enable useful statistics information for the ehcache implementation in your Java application:

  1. Define two new beans – one for ehcacheManagementService and the other for mbeanServer – in the applicationContext file. Make sure that registerCacheStatistics constructor argument is set to true.
    <bean id="ehcacheManagementService" class="net.sf.ehcache.management.ManagementService">
          <constructor-arg name="cacheManager" ref="ehcache"/>
          <constructor-arg name="mBeanServer" ref="mbeanServer"/>
          <constructor-arg name="registerCacheManager" value="false"/>
          <constructor-arg name="registerCaches" value="false"/>
          <constructor-arg name="registerCacheConfigurations" value="false"/>
          <constructor-arg name="registerCacheStatistics" value="true"/>
    </bean>
    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
          <property name="locateExistingServerIfPossible" value="true"/>
    </bean>
    <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"/>
    <cache:annotation-driven/>
    
  2. Set statistics property of your cache in ehcache.xml to true.
    <cache name="coreCache"
             maxElementsInMemory="10000"
             eternal="false"
             timeToIdleSeconds="86400"
             timeToLiveSeconds="86400"
             overflowToDisk="true"
             maxElementsOnDisk="10000000"
             diskPersistent="false"
             diskExpiryThreadIntervalSeconds="86400"
             memoryStoreEvictionPolicy="LRU"
             statistics="true">
      </cache>
    

That’s it! Ehcache mbeans are now exposed to be consumed by any JMX-compatible monitoring tool, viz., JConsole or VisualVM (with Java Profiler plugin) to name a few.

Here is a screenshot of the Ehcache statistics in VisualVM:

Screen Shot 2014-08-08 at 23.33.18

Advertisements