How to set the name of the net.sf.ehcache.CacheManager for JMX monitoring?

Bogdan Minciu picture Bogdan Minciu · Nov 21, 2011 · Viewed 9.1k times · Source

I am using EhCache 1.4.0, Spring 3.0.5 in a web application deployed on Tomcat 6 using JRE 1.6. I am exposing via JMX the L2 cache management, like this:

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

<util:property-path id="hibernateCacheProvider" path="sessionFactory.settings.cacheProvider" />

<bean id="hibernateEhCacheManager" class="com.mycompany.spring.beans.factory.config.UnaccessibleFieldRetrievingFactoryBean">
    <property name="targetObject" ref="hibernateCacheProvider" />
    <property name="targetField" value="manager" />
    <property name="makeInstanceFieldVisible" value="true" />
</bean>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <description>The cacheManager configuration.</description>
    <property name="targetClass" value="net.sf.ehcache.management.ManagementService" />
    <property name="staticMethod" value="net.sf.ehcache.management.ManagementService.registerMBeans" />
    <property name="arguments">
        <list>
            <ref bean="hibernateEhCacheManager" />
            <ref bean="mbeanServer" />
            <value type="boolean">true</value>
            <value type="boolean">true</value>
            <value type="boolean">true</value>
            <value type="boolean">true</value>
        </list>
    </property>
</bean>

<bean class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter">
    <property name="server" ref="mbeanServer" />
    <property name="beans">
        <map>
            <entry key="Hibernate:type=statistics,application=applicationOne">
                <bean class="org.hibernate.jmx.StatisticsService">
                    <property name="statisticsEnabled" value="true" />
                    <property name="sessionFactory" ref="sessionFactory" />
                </bean>
            </entry>
        </map>
    </property>
</bean>

<bean id="hbm.properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.generate_statistics">false</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
            <prop key="hibernate.cache.provider_configuration_file_resource_path">applicationOne-web/ehcache.xml</prop>
            <prop key="hibernate.cache.query_cache_factory">org.hibernate.cache.StandardQueryCacheFactory</prop>
        </props>
    </property>
</bean>

I have to allow to clear all the entries in the L2 cache by using the jmxterm tool, like this:

run --bean net.sf.ehcache:type=CacheManager,name=net.sf.ehcache.CacheManager@605df3c5 clearAll

I am aware of jconsole to determine the exact CacheManager, from the context, but I may not use it for some reasons I won't get into.

So far, so good, but suppose that my JVM (Tomcat server) has 2 applications deployed, both allowing JMX monitoring for EhCache. The names of these two MBeans will be:

net.sf.ehcache:type=CacheManager,name=net.sf.ehcache.CacheManager@605df3c5
net.sf.ehcache:type=CacheManager,name=net.sf.ehcache.CacheManager@49ff3459

As you can see they are not quite useful when trying to determine which cache to clear.

So my question is: is there any possibility to set the name of each CacheManager, in order to identify exactly which one to use to clear all the entries in the L2 cache ?

Thank you.

Answer

lane.maxwell picture lane.maxwell · Apr 30, 2013

I know this was answered long ago, but I think it is easier just to set it in your ehcache configuration file (applicationOne-web/ehcache.xml).

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
    monitoring="autodetect" dynamicConfig="true" name="MY_CACHE_MANAGER_NAME">

...
</ehcache>