I found a simpler way using a scheduled service to log stats:
@Singleton
@Startup
@SuppressWarnings("checkstyle:designforextension") // CDI beans not allowed
to have final methods
public class CacheStatService {
/**
* Logger.
*/
@SuppressWarnings({"checkstyle:constantname",
"PMD.VariableNamingConventions"}) // Logger OK to be static final
and lower case
private static final Logger log = LoggerFactory.getLogger(
CacheStatService.class);
@Schedule(second = "*", minute = "*/1", hour = "*", persistent = false)
public void logCacheStats() {
try {
// Get MBean server list
final ArrayList list = MBeanServerFactory.findMBeanServer(null);
// Grab first one
final MBeanServer mbeanServer = (MBeanServer) list.get(0);
// Name of the cache in JCache
final ObjectName objectName = new ObjectName(
"javax.cache:type=CacheStatistics,CacheManager=hazelcast,Cache=header");
// Get MBean info
final MBeanInfo info = mbeanServer.getMBeanInfo(objectName);
// Get array of attributes
final MBeanAttributeInfo[] attributes = info.getAttributes();
final Map<String, Object> attrMap = new HashMap<>();
// Get all attributes
for (final MBeanAttributeInfo attr : attributes) {
attrMap.put(attr.getName(),
mbeanServer.getAttribute(objectName,
attr.getName()));
}
log.info(String.format("Cache stats: %s", attrMap));
} catch (MalformedObjectNameException | InstanceNotFoundException |
IntrospectionException | ReflectionException |
MBeanException |
AttributeNotFoundException e) {
log.error(e.getMessage());
}
}
08/05/2016 14:06:05.001 [EjbTimerPool - 1] INFO
com.bhn.services.CacheStatService - Cache stats: {CacheGets=10,
AverageGetTime=61.4972, CacheRemovals=0, CacheHitPercentage=80.0,
AveragePutTime=376.1885, CacheMissPercentage=20.0, AverageRemoveTime=0.0,
CachePuts=4, CacheEvictions=0, CacheMisses=2, CacheHits=8}
--
View this message in context:
http://tomee-openejb.979440.n4.nabble.com/JCache-stats-without-JMX-tp4679621p4679636.html
Sent from the TomEE Users mailing list archive at Nabble.com.