-
-
Save theotherian/7540383 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Guava is just better with JMX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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