I have used JCache with EhCache as provider. Using the MBean I am able to get cache statistics but there is missing attributes like size of the cache. I see LiveCacheStatistics provide that but how to get instance of it from JCache
List<Map<String,Object>> cacheStatasticsList = new ArrayList<>();
try {
final MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
final Set<ObjectInstance> cacheBeans = beanServer.queryMBeans(ObjectName.getInstance("javax.cache:type=CacheStatistics,CacheManager=*,Cache=*"), null);
for (ObjectInstance cacheBean : cacheBeans) {
final CacheStatisticsMXBean cacheStatisticsMXBean =
MBeanServerInvocationHandler.newProxyInstance(beanServer, cacheBean.getObjectName(), CacheStatisticsMXBean.class, false);
Map<String,Object> cacheStatasticsMap = new HashMap<>();
cacheStatasticsMap.put("Name",cacheBean.getObjectName().getKeyProperty("Cache"));
cacheStatasticsMap.put("Gets",cacheStatisticsMXBean.getCacheGets());
cacheStatasticsMap.put("Hits",cacheStatisticsMXBean.getCacheHits());
cacheStatasticsMap.put("Misses",cacheStatisticsMXBean.getCacheMisses());
cacheStatasticsMap.put("Removals",cacheStatisticsMXBean.getCacheRemovals());
cacheStatasticsMap.put("Evictions", cacheStatisticsMXBean.getCacheEvictions());
cacheStatasticsMap.put("AvgGetTime", cacheStatisticsMXBean.getAverageGetTime());
cacheStatasticsMap.put("AvgPutTime", cacheStatisticsMXBean.getAveragePutTime());
cacheStatasticsMap.put("AvgRemoveTime" , cacheStatisticsMXBean.getAverageRemoveTime());
cacheStatasticsList.add(cacheStatasticsMap);
}
} catch(Exception e){
log.error("Error in getting cache statistics.");
return cacheStatasticsList;
}
return cacheStatasticsList;
Unfortunately, the JSR107 standard does not define a size in the statistics object. See: CacheStatisticsMXBean
Some reasons that I recall from discussions with the expert group and spec leads and from my own experience as cache implementer:
Although it is very painful, that the size is missing in the statistics, there are reasons not to include it. The metrics that are available now, are just a read out from counters. Depending on the implementation, to come up with the size, some heavier operations are needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With