gpt4 book ai didi

java - EhCache 3 : How to unwrap statistics bean?

转载 作者:行者123 更新时间:2023-11-30 01:46:45 25 4
gpt4 key购买 nike

<?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 来显示统计信息,但我不知道如何操作,因为在网上我只能看到以编程方式注入(inject)的 Bean(另请参阅此 SO 问题)。

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

有什么建议吗?

最佳答案

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

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

关于java - EhCache 3 : How to unwrap statistics bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57669349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com