Skip to content

Instantly share code, notes, and snippets.

@brettwooldridge
Created December 5, 2014 01:35
Show Gist options
  • Save brettwooldridge/405d7c54784fc3a99813 to your computer and use it in GitHub Desktop.
Save brettwooldridge/405d7c54784fc3a99813 to your computer and use it in GitHub Desktop.
HikariCP JMX Access
package com.zaxxer.hikari;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
public class HikariJmxElf
{
private final ObjectName poolAccessor;
private final MBeanServer mBeanServer;
public HikariJmxElf(final String poolName)
{
try {
mBeanServer = ManagementFactory.getPlatformMBeanServer();
poolAccessor = new ObjectName("com.zaxxer.hikari:type=Pool (" + poolName + ")");
}
catch (MalformedObjectNameException e) {
throw new RuntimeException("Pool " + poolName + " could not be found", e);
}
}
public int getIdleConnections()
{
try {
return (Integer) mBeanServer.getAttribute(poolAccessor, "IdleConnections");
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
public int getActiveConnections()
{
try {
return (Integer) mBeanServer.getAttribute(poolAccessor, "ActiveConnections");
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
public int getTotalConnections()
{
try {
return (Integer) mBeanServer.getAttribute(poolAccessor, "TotalConnections");
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment