Monday, June 13, 2011

Groovy JMX MBean Viewer instead of JConsole

You might be monitoring something with JConsole, and thinking, how can I automate this check?

Answer: With Groovy!

Here is an example of how easy it is to probe an ActiveMQ MBean that sits on 2 App Servers.

import javax.management.remote.*

['11.12.13.123', '11.12.13.124'].each { serverIp ->
   def server = jmxConnect(serverIp)
   def mbean = new GroovyMBean(server, 'org.apache.activemq:BrokerName=appEventBroker,Type=Queue,Destination=appEventQueue')
   println "On app server $serverIp"
   printQueueStatus(mbean)
}

def jmxConnect(serverIp) {
   def url = "service:jmx:rmi:///jndi/rmi://$serverIp:8999/jmxrmi"
   def env = [(JMXConnector.CREDENTIALS): (String[])['myRole', 'myPassword']]
   def connection = JMXConnectorFactory.connect(new JMXServiceURL(url), env)
   def server = connection.MBeanServerConnection
   return server
}

def printQueueStatus(mbean) {
   println "DequeueCount: $mbean.DequeueCount"
   println "EnqueueCount: $mbean.EnqueueCount"
}

1 comment:

  1. I have forgotten you could also do JMX with groovy. I guess my first thought should always be: "Can you use Groovy to get it done?". Thanks for sharing!

    ReplyDelete