Sundararajan's Weblog

  • Java
    August 2, 2006

Groovier jconsole!

Guest Author

I blogged about script shell plugin for jconsole.
With script shell plugin, you can use JavaScript to view/analyze JMX MBeans. This is particularly useful if you are going to debug your own MBeans -- although jconsole is a generic JMX client, you can use scripting to debug your MBeans. But, what if your scripting language of choice is not JavaScript? How about Groovy? It turns out that it is possible to use any language for which you have JSR-223 script engine.

Using Groovy with jconsole

Steps to use Groovy with jconsole script shell/console plugin:
  1. download https://mustang.dev.java.net :-)
  2. download Groovy - available from http://dist.codehaus.org/groovy/distributions/ (groovy-all-1.0-JSR-06.jar)
  3. download JSR-223 script engine for Groovy - available from https://scripting.dev.java.net (groovy-engine.jar)
  4. Drop both groovy-all-1.0-JSR-06.jar and groovy-engine.jar in $JDK_HOME/jre/lib/ext - note that in general it is not advisable to drop arbitrary jar files under JRE's ext directory -- there are security implications. But, this is just for experiment!
  5. Now, use the following command to start jconsole:

    jconsole -J-Dcom.sun.demo.jconsole.console.language=groovy -pluginpath $JDK_HOME/demo/scripting/jconsole-plugin/jconsole-plugin.jar

With -J-Dcom.sun.demo.jconsole.console.language=groovy option, you are specifying to use Groovy as the language for script shell plugin. You will see something like this:

There is MBean support in Groovy -- you may want to refer to the class groovy.util.GroovyMBean. Here is the Groovy "session" with jconsole:(Note: plugin is a pre-defined variable of type JConsolePlugin)

pid: 1876 java2demo.jar
groovy>conn = plugin.context.getMBeanServerConnection()
groovy>def objectName(str) { return new javax.management.ObjectName(str); }
groovy>def mbean(str) { return new groovy.util.GroovyMBean(conn, objectName(str)); }
groovy>classLoading = mbean("java.lang:type=ClassLoading")
MBean Name:
(r) int LoadedClassCount
(r) long TotalLoadedClassCount
(r) long UnloadedClassCount
(rw) boolean Verbose
groovy>memoryMBean = mbean("java.lang:type=Memory")
MBean Name:
(r) javax.management.openmbean.CompositeData HeapMemoryUsage
(r) javax.management.openmbean.CompositeData NonHeapMemoryUsage
(r) int ObjectPendingFinalizationCount
(rw) boolean Verbose
void gc()

You can customize further by defining a file named jconsole.groovy under your home directory. This will be
loaded by script shell plugin just after initialization. You can define classes, functions in jconsole.groovy that could be called interactively in groovy script shell prompt.

Join the discussion

Comments ( 1 )
  • Andres Almiray Wednesday, August 2, 2006
    Cool! I've worked with JMX before, found it useful but sometimes awkward to work with. Now with Groovy things seems a lot simplier. Thanks for the tip!
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.