Skip to content

Instantly share code, notes, and snippets.

@chokkoyamada
Last active December 16, 2015 20:29
Show Gist options
  • Save chokkoyamada/5492816 to your computer and use it in GitHub Desktop.
Save chokkoyamada/5492816 to your computer and use it in GitHub Desktop.
count tomcat busy threads by groovy JMX
import javax.management.ObjectName
import javax.management.remote.JMXConnectorFactory as JmxFactory
import javax.management.remote.JMXServiceURL as JmxUrl
serverUrl = 'service:jmx:rmi:///jndi/rmi://localhost:9013/jmxrmi'
conn = JmxFactory.connect(new JmxUrl(serverUrl)).MBeanServerConnection
def query = new ObjectName('Catalina:*')
String[] allNames = conn.queryNames(query, null)
def modules = allNames.findAll{
name ->
name.contains('Catalina:type=ThreadPool')
}.collect{ new GroovyMBean(conn, it) }
def busyThreadsCount = 0
modules.each{ m ->
def obj = new ObjectName(m.name() as String)
def ret = conn.getAttribute(obj, "currentThreadsBusy")
busyThreadsCount = busyThreadsCount + ret
}
println(busyThreadsCount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment