【问题标题】:How to set the name of the net.sf.ehcache.CacheManager for JMX monitoring?JMX监控如何设置net.sf.ehcache.CacheManager的名字?
【发布时间】:2011-11-21 14:59:34
【问题描述】:

我在使用 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 工具清除二级缓存中的所有条目,如下所示:

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 缓存中的所有条目?

谢谢。

【问题讨论】:

    标签: java spring jmx ehcache mbeans


    【解决方案1】:

    我知道这个问题很久以前就已经回答过了,但我认为在您的 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>
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-03
        • 1970-01-01
        • 1970-01-01
        • 2014-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多