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.
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.
Posted by Andres Almiray on August 02, 2006 at 10:25 PM IST #