BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News JMX the Ruby way with jmx4r

JMX the Ruby way with jmx4r

This item in japanese

One of the benefits of running on a mature platform such as Java is the availability of, well, mature features such as monitoring. Ola Bini, JRuby Core team member, found this to be useful for monitoring the memory behavior of a JRuby application:
You get this for free, just by running JRuby with Java 6. You can attach to any Java process at all. Remotely too. And get this kind of information. Can your Ruby do that?
The tool he used to monitor the JVM process was JConsole, which has been shipped with Java since version 5. Another way of accessing JMX information is now available: jmx4r by Jeff Mesnil allows to access JMX MBeans from JRuby code. Here a simple example:
require 'java'
require 'jmx4r'
memory = JMX::MBean.find_by_name "java.lang:type=Memory"
memory.verbose = true
memory.gc
This connects to an MBean server on localhost with default connection parameters, but it's possible to use a custom JMX Service URL as well.

Jmx4r removes boilerplate from JMX client code using Ruby Metaprogramming techniques. This allows a Ruby-ish way of accessing the JMX operations and attributes of the MBeans.

In the sample, memory.verbose is an attribute which would normally have to be set with a verbose JMX method call. Jmx4r sets up an accessors in the class that represents the memory MBean. Information about the MBean is fetched and define_method is used to create the necessary methods for every attribute.

Operations are supported with the help of the method_missing, which is called when no method definition for a method call can be found. In the sample, memory.gc is a call to the Memory MBean's operation that runs the Garbage Collector - but such a method is not defined in the class of the memory object. Instead, the method_missing method is invoked, determines if there is an operation for this method name, and then invokes it using JMX APIs.

A big advantage of a library such as jmx4r comes into play with interactive shells for JRuby, such as jirb. This allows a developer or JRuby savy admin to access one (or more) MBean servers while still having the full power of a language available. This is useful for bulk jobs which haven't been automated otherwise. Another example from the jmx4r site shows this:
logging = JMX::MBean.find_by_name "java.util.logging:type=Logging"
logging.logger_names.each do |logger_name|
 logging.set_logger_level logger_name, "INFO"
end

This queries all loggers and changes their log level in one fell swoop. In a graphical tool, this would require a few clicks per logger. No to mention that, once this code is written and known to work, it can be saved off into a script and reused. Another benefit of this is the ability for a JRuby process to monitor its own JVM by contacting its MBean server.

A recently added feature is support for authentication. Here a sample of how to use this:
JMX::MBean.establish_connection :host => "localhost", 
 :username => "jeff", :password => "secret"

Rate this Article

Adoption
Style

BT