java - 如何设置JMX监控的net.sf.ehcache.CacheManager名称?

标签 java spring jmx ehcache mbeans

我在使用 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 的名称,以便准确识别使用哪个 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>

关于java - 如何设置JMX监控的net.sf.ehcache.CacheManager名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8213805/

相关文章:

java - Java实现文件上传类型

java - 如何在不同的类布局中显示呈现的对象?

java - 如果有未提交的更改,Maven 会阻止构建

java - 如何在其他 JTextField 下找到 JTextField?

java - PUT 请求 : Required request body is missing

java - Weblogic MBean 服务器仅在 15 秒后提供该值

java - 缺少 Websphere NodeAgent JMX MBean

java - Spring注入(inject)的DataSource为空

java - 如何在spring webflow中实现Switch/if else if语句?

java - dropwizard.metrics :metrics-core latest release 中 JmxReporter 的替代品