如何在jmx中显示EhCache的统计信息

时间:2014-07-22 10:32:34

标签: spring caching jmx ehcache

我设置了一些缓存来测试某些DTO的缓存。现在,我想查看 jconsole 中缓存中的统计信息。 问题是他们未提供

也许,缓存无法正常工作,这就是统计数据中没有值的原因。但我没有看到代码中的任何错误或当我启动服务器时,所以我认为它们必须正常工作。

我在 spring.xml中的代码:

<!-- ******************** EhCache DTO caching ************************ -->

<ehcache:annotation-driven create-missing-caches="true" cache-manager="dtoCacheManager" />

<bean id="dtoCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
    p:config-location="dtoEhcache.xml" p:shared="true"/>



<!-- ***** register cache with JMX ***** -->

<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
    <property name="locateExistingServerIfPossible" value="true" />
</bean>


<bean id="ehCacheMBeanRegistration" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod" value="net.sf.ehcache.management.ManagementService.registerMBeans"/>
    <property name="arguments">
        <list>
            <ref bean="dtoCacheManager"/>
            <ref bean="mbeanServer"/>
            <value>true</value>
            <value>true</value>
            <value>true</value>
            <value>true</value>
        </list>
    </property>
</bean>

我在 dtoEhcache.xml中的代码:

<!-- ***** Config for the Caches ***** -->

<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
monitoring="autodetect" dynamicConfig="true" updateCheck="false">

<diskStore path="java.io.tmpdir/DtoEhCacheSpring" />

<defaultCache eternal="false" maxElementsInMemory="1000"
    overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
    timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" statistics="true" />

<cache name="brandDataCache" eternal="false" maxElementsInMemory="1000"
    overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
    timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" statistics="true" />

<cache name="cmsSiteCache" eternal="false" maxElementsInMemory="1000"
    overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
    timeToLiveSeconds="1000" memoryStoreEvictionPolicy="LRU" statistics="true" />

<cache name="contentPageCache" eternal="false"
    maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false"
    timeToIdleSeconds="0" timeToLiveSeconds="1000"
    memoryStoreEvictionPolicy="LRU" statistics="true" />

应缓存的方法示例:

 @Cacheable(cacheName = "brandDataCache")
public BrandData convert(final BrandModel brandModel, final BrandData prototype) throws ConversionException {
... impl ... }

1 个答案:

答案 0 :(得分:0)

我发现了错误。这不是jmx配置的问题。放置可缓存注释是一个问题。如果从类外部调用该方法,则缓存将仅为triggert。我错误地将它放在一个只在类中调用的方法(facepalm)。我将注释更改为另一种方法,现在它可以正常工作。