Skip to content

Instantly share code, notes, and snippets.

@theotherian
Forked from kofemann/GuavaCacheMXBean.java
Last active July 31, 2017 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save theotherian/7540383 to your computer and use it in GitHub Desktop.
Save theotherian/7540383 to your computer and use it in GitHub Desktop.
Guava is just better with JMX
public interface GuavaCacheMXBean<K, V> {
public long getRequestCount();
public long getHitCount();
public double getHitRate();
public long getMissCount();
public double getMissRate();
public long getLoadCount();
public long getLoadSuccessCount();
public long getLoadExceptionCount();
public double getLoadExceptionRate();
public long getTotalLoadTime();
public double getAverageLoadPenalty();
public long getEvictionCount();
public long getSize();
public void cleanUp();
public void invalidateAll();
public void refreshAll();
}
import java.util.concurrent.ConcurrentMap;
/**
* A JMX wrapper for google's Guava Cache
*/
public class GuavaCacheMXBeanImpl<K, V> implements GuavaCacheMXBean<K, V> {
private final com.google.common.cache.LoadingCache<K, V> cache;
public GuavaCacheMXBeanImpl(com.google.common.cache.LoadingCache<K, V> cache) {
this.cache = cache;
}
@Override
public long getRequestCount() {
return cache.stats().requestCount();
}
@Override
public long getHitCount() {
return cache.stats().hitCount();
}
@Override
public double getHitRate() {
return cache.stats().hitRate();
}
@Override
public long getMissCount() {
return cache.stats().missCount();
}
@Override
public double getMissRate() {
return cache.stats().missRate();
}
@Override
public long getLoadCount() {
return cache.stats().loadCount();
}
@Override
public long getLoadSuccessCount() {
return cache.stats().loadSuccessCount();
}
@Override
public long getLoadExceptionCount() {
return cache.stats().loadExceptionCount();
}
@Override
public double getLoadExceptionRate() {
return cache.stats().loadExceptionRate();
}
@Override
public long getTotalLoadTime() {
return cache.stats().totalLoadTime();
}
@Override
public double getAverageLoadPenalty() {
return cache.stats().averageLoadPenalty();
}
@Override
public long getEvictionCount() {
return cache.stats().evictionCount();
}
@Override
public long getSize() {
return cache.size();
}
@Override
public void cleanUp() {
cache.cleanUp();
}
@Override
public void invalidateAll() {
cache.invalidateAll();
}
@Override
public void refreshAll() {
ConcurrentMap<K,V> map = cache.asMap();
for (K key : map.keySet()) {
cache.refresh(key);
}
}
}
import java.lang.management.ManagementFactory;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import com.google.common.cache.LoadingCache;
public final class LoadingCacheJMXManager {
private LoadingCacheJMXManager() {}
public static <K, V> void registerLoadingCacheInJMX(String cacheName, LoadingCache<K, V> cache) {
String name = String.format("%s:type=%s,name=%s",
cache.getClass().getPackage().getName(), cache.getClass().getSimpleName(), cacheName);
try {
Object mBean = new GuavaCacheMXBeanImpl<K, V>(cache);
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName mxBeanName = new ObjectName(name);
if (!server.isRegistered(mxBeanName)) {
server.registerMBean(mBean, mxBeanName);
}
} catch (MalformedObjectNameException | InstanceAlreadyExistsException | MBeanRegistrationException
| NotCompliantMBeanException ex) {
throw new IllegalStateException(
String.format("An exception was thrown registering the JMX bean with the name '%s'", name), ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment