Groovier jconsole!

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)


groovy>plugin.context
pid: 1876 java2demo.jar
groovy>conn = plugin.context.getMBeanServerConnection()
javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection@16c1bce
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:
  java.lang:type=ClassLoading
  
Attributes:
  (r) int LoadedClassCount
  (r) long TotalLoadedClassCount
  (r) long UnloadedClassCount
  (rw) boolean Verbose
groovy>classLoading.LoadedClassCount
2571
groovy>classLoading.UnloadedClassCount
105
groovy>memoryMBean = mbean("java.lang:type=Memory")
MBean Name:
  java.lang:type=Memory
  
Attributes:
  (r) javax.management.openmbean.CompositeData HeapMemoryUsage
  (r) javax.management.openmbean.CompositeData NonHeapMemoryUsage
  (r) int ObjectPendingFinalizationCount
  (rw) boolean Verbose
Operations:
  void gc()
groovy>

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.

Comments:

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!

Posted by Andres Almiray on August 02, 2006 at 03:25 PM IST #

Post a Comment:
Comments are closed for this entry.
About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder