Friday April 20, 2007 ![]() |
JMX, SNMP, Java, etc...Daniel Fuchs blogs on JMX, SNMP, Java, etc... |
A few weeks ago, I posted a small example showing
how to programatically retrieve the JVM Management and Monitoring
information. Jeff Mesnil
also recently wrote a two parts blog article showing how to write a JMX client using JRuby.
JConsole Script Shell PluginSince Java SE 6, JConsole exposes a plugin API that will let you develop your own Plugin Tab. Java SE 6 comes with a scripting demo that contains a generic Script Shell tab, which uses JSR 223 ScriptEngine API to instantiate the ScriptEngine of your choice. Sundar already blogged about using JavaScript and Groovy with this Script Shell tab. I will also update my entry about BeanShell, showing how to use the BeanShell ScriptEngine with JConsole. But what interest me today is to show how to expand on Jeff's article in order to show how you could prototype your ruby script to access the JVM Management and Monitoring information from within JConsole Script Shell tab. Starting JConsole with a JRuby Script Shell tabThis is an easy step when you know how to do it and what pitfall to avoid. As it happens, the error message that you get back if things go wrong is a bit laconic, so it's better to know in advance what is suposed to work and what isn't. First you will need to download several things (unless you already have them):
Create a scripts folder somewhere and unzip the
Then expand the jruby-bin-0.9.8 archive you got from
http://jruby.codehaus.org/ in that same scripts folder.
You will now find the jruby implementation jars in a
To start jconsole from the scripts folder use the following command:
Be carefull not to include any empty element when specifying
the classpath - something like e.g.
When JConsole has started, connect to a target VM. After the connection is established, you should notice a Script Shell tab:
Click on the Script Shell tab and you'll be able to enter Ruby commands.
The MBeanServerConnection to the target VM can be obtained from the
For instance, to count the MBeans in the target VM you just need to call:
Or to get the list of MBeans registered in the target VM:
Assigning variablesOne of the pitfalls I immediately fell into was when I tried to save the connection to the target VM in a local variable. Each line of command that you type in is executed in its own local scope, and therefore, any local variable you try to set will be accessible only within that scope, i.e. within that line. So for instance:
works, but the following doesn't:
Here again, the error message you get when jruby raises an exception is a bit laconic ("org.jruby.exceptions.RaiseException") so it took me a while to figure out what was going wrong. The bottom line is: if you want to be able to assign a variable on one line, and use it in the next, you must use a global variable assignation:
Creating Proxies to access Platform MXBeansSo now we are ready to get back to Jeff's example:
... and that's it! Nice, isn't it? cheers, Update: I have also come across this nice article by Paul King, this time about using JMX with Groovy. Quoting: Given that Groovy sits directly on top of Java, Groovy can leverage the tremendous amount of work already done for JMX with Java. Update 2: I also came across this blog entry by Ivan who explains how to manage GlassFish using JRuby. Tags: java jconsole jmx jruby jvm monitoring rubyPosted by dfuchs ( Apr 20 2007, 05:40:13 PM CEST ) Permalink Comments [0] |