【问题标题】:EhCache 3: How to unwrap statistics bean?EhCache 3:如何解开统计 bean?
【发布时间】:2019-08-27 07:07:58
【问题描述】:
<?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 问题)。

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

有什么建议吗?

【问题讨论】:

    标签: java jcache ehcache-3


    【解决方案1】:

    您启用了 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");
    

    请注意,JCache Cache 的创建方式与您在问题中的创建方式不同。在此处查看大量文档: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

    【讨论】:

      猜你喜欢
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 2015-08-18
      相关资源
      最近更新 更多