EhCache 3:如何解包统计bean?
EhCache 3: How to unwrap statistics bean?
<?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();
有什么建议吗?
您启用了 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
<?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();
有什么建议吗?
您启用了 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