Created
December 5, 2014 01:35
-
-
Save brettwooldridge/405d7c54784fc3a99813 to your computer and use it in GitHub Desktop.
HikariCP JMX Access
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
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