繁体   English   中英

如何为JMX监视设置net.sf.ehcache.CacheManager的名称?

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

我正在使用JRE 1.6在Tomcat 6上部署的Web应用程序中使用EhCache 1.4.0,Spring 3.0.5。 我通过JMX公开了L2缓存管理,如下所示:

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

我必须允许使用jmxterm工具清除L2缓存中的所有条目,如下所示:

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

我知道jconsole可以根据上下文确定确切的CacheManager ,但是由于某些原因,我可能不会使用它,因此我不会使用它。

到目前为止,一切都很好,但是假设我的JVM(Tomcat服务器)已部署了2个应用程序,都允许对EhCache进行JMX监视。 这两个MBean的名称为:

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

如您所见,在尝试确定要清除的缓存时,它们并不是很有用。

所以我的问题是:是否有可能设置每个CacheManager的名称,以便准确地确定使用哪个来清除L2缓存中的所有条目?

谢谢。

我知道很早以前就已经回答了,但是我认为将其设置在您的ehcache配置文件(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>

一旦hibernateEhCacheManager可用,就可以使用以下bean定义调用其方法(设置包含)。 通常,这应该可以重命名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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM