Sunday, September 20, 2009

Monitoring EhCache with JMX and Spring

EhCache provides an easy way to manage your caches with JMX. The ManagementService class is the API entry point. You can easily integrate it in your Spring configuration files.

Here's how you can do it :

<ehcache:config configLocation="classpath:config/ehcache.xml" failQuietly="true" />

<ehcache:caching id="yourCacheModel" cacheName="yourCache"/>

<bean id="mbeanServer" class="">
<property name="locateExistingServerIfPossible" value="true" />

<bean class="" init-method="init">
<constructor-arg ref="cacheManager" />
<constructor-arg ref="mbeanServer" />
<constructor-arg value="true" />
<constructor-arg value="true" />
<constructor-arg value="true" />
<constructor-arg value="true" />

After doing this, you will have access to your caches configuration and operations such as cache flushing. You will also have access to useful statistics like cache hits, cache misses, object count and some more. These statistics will give you critical information on how to configure your caches for an optimal use.

Resources :
- JMX Management and Monitoring from the ehcache official documentation.


  1. Julien,
    What does "ref='cacheManager'" referring to?

  2. The use of the annotation ehcache:config will create a bean named cacheManager.

  3. Julien, what schemaLocations are you using. I'm getting:
    Attribute 'configLocation' is not allowed to appear in element 'ehcache:config'.

  4. Hi,

    I'm using :




  5. Hello,

    We have packaged EhCache JMX registration in a compact Spring XML namespace based configuration declaration :

    cache-manager="cacheManager" />

    We also published an Hyperic HQ plugin to monitor EhCache caches (hit/miss ratio, size, etc).

    Details are here.

    This util is part of larger Open Source JMX/Management library we called xebia-management-extras that covers Jakarta Commons DBCP dataSource, util.concurrent ExecutorService, JMS (connection factory, Spring's DefaultMessageListenerContainer, etc), CXF and a @Profiled annotation to ease monitoring of business code.

    All the details are at Google Code - Xebia Management Extras.

    Hope this helps,


  6. What do you do if have more than one cacheManager to monitor? I am using another for method level caching and I want to monitor that one as well.

  7. On that ManagementService bean you should set the destroy-method property to "dispose". Otherwise if your Spring context shuts down but your server doesn't (like in the case of a hot deploy), you'll get errors about duplicate MBeans.