How to set net.sf.ehcache.CacheManager name to monitor JMX?

I am using EhCache 1.4.0, Spring 3.0.5 in a web application deployed on Tomcat 6 using JRE 1.6. I look through JMX L2 cache management, for example:

<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 must allow to clear all entries in the L2 cache using the jmxterm tool, for example:

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

I know jconsole to determine the exact CacheManager , out of context, but I cannot use it for some reason, I will not get in.

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

 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 very useful when trying to determine which cache is being cleared.

So my question is: is it possible to set the name of each CacheManager to determine exactly which one to use to clear all entries in the L2 cache?

Thanks.

+6
source share
2 answers

Once hibernateEhCacheManager is available, you can call its methods (by setting the inclusion) using the following bean definition. Usually this should do the trick renaming the CacheManager.

  <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <ref local="hibernateEhCacheManager"/> </property> <property name="targetMethod"> <value>setName</value> </property> <property name="arguments" value="<the_desired_name>"/> </bean> 
+2
source

I know this was given a long time ago, but I think it’s easier to just install it in my 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> 
+13
source

Source: https://habr.com/ru/post/902010/


All Articles