2011-11-21 73 views
6

我在使用JRE 1.6的Tomcat 6上部署的Web应用程序中使用EhCache 1.4.0,Spring 3.0.5。我通过JMX暴露的L2高速缓存管理,像这样:如何为JMX监控设置net.sf.ehcache.CacheManager的名称?

<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,[email protected] clearAll 

我我知道jconsole从上下文确定确切的CacheManager,但由于某些原因,我可能不会使用它,因为我不会介入。

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

net.sf.ehcache:type=CacheManager,[email protected] 
net.sf.ehcache:type=CacheManager,[email protected] 

正如你可以看到他们都不太有用尝试确定要清除哪个缓存时。

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

谢谢。

回答

2

一旦hibernateEhCacheManager可用,可以使用下面的bean定义调用它的方法(设置includes)。通常这应该重新命名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> 
13

我知道这是很久以前回答,但我认为这是更容易只是把它在你的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>