1
<?xml version="1.0" encoding="UTF-8"?>
<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
        xmlns='http://www.ehcache.org/v3'
        xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.1.xsd">

    <service>
        <jsr107:defaults enable-management="false" enable-statistics="true"/>
    </service>

    <cache alias="mySlidingExpiryJCache">
        <key-type>java.lang.Long</key-type>

        <expiry>
            <tti unit="seconds">2</tti>
        </expiry>
        <resources>
            <heap unit="entries">200</heap>
        </resources>
        <jsr107:mbeans enable-statistics="true"/>
    </cache>
</config>

我想通过提取 MBean 来显示统计信息,但是我不知道如何,因为在网上我只能看到以编程方式注入的 bean(另请参见这个SO question)。

StatisticsService statisticsService = new DefaultStatisticsService();
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .using(statisticsService)
        .build();
cacheManager.init();

有什么建议吗?

4

1 回答 1

2

您启用了 JSR107/JCache 统计信息。这些可通过 JMX 获得。如果您想以编程方式访问这些 JMX bean,您可以执行以下操作:

Cache cache = // a JSR107 cache
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("javax.cache:type=CacheStatistics," +
  "CacheManager=" + cache.getCacheManager().getURI().toString() +
  ",Cache=" + mbeanSafe(cache.getName()));
long hits = mBeanServer.getAttribute(name, "CacheHits");

请注意,JCacheCache的创建方式与您在问题中的创建方式不同。请参阅此处的大量文档:https ://www.ehcache.org/documentation/3.0/107.html

JSR107/JCache 是许多 Java 缓存支持的标准 API。它还包括通过 JMX 公开统计信息。可用指标定义在:https ://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/management/CacheStatisticsMXBean.java

于 2019-08-27T09:16:06.720 回答