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:annotations>
<ehcache:caching id="yourCacheModel" cacheName="yourCache"/>
</ehcache:annotations>

<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="locateExistingServerIfPossible" value="true" />
</bean>

<bean class="net.sf.ehcache.management.ManagementService" 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" />
</bean>

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.

8 comments:

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

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

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

    ReplyDelete
  4. Hi,

    I'm using :

    xmlns:ehcache="http://www.springmodules.org/schema/ehcache"

    and

    schemaLocation="http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"

    ReplyDelete
  5. Hello,

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


    <management:eh-cache-management-service
    mbean-server="mbeanServer"
    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,

    Cyrille

    ReplyDelete
  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.
    Thx

    ReplyDelete
  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.

    ReplyDelete