Drawing Hands by M.C. Escher (1948)

Jeff Mesnil


jmx4r, a JMX Libary for JRuby

On June 11th, 2007 in java, jmx, jmx4r, jruby, ruby

Just in time for the release of JRuby 1.0 and following my experiments with writing JRuby scripts to manage remote applications using JMX (part I & II), I created jmx4r, a simple library which makes it super easy to write such scripts.

For example, to trigger a Garbage Collection on a remote Java application , the whole script is:

require 'java'
require 'jmx4r'

memory = JMX::MBean.find_by_name "java.lang:type=Memory"
memory.verbose = true
memory.gc

Simple enough, isn’t it?

JMX Connection

By default, jmx4r expects to connect to the remote MBean Server on localhost using the standard JMX Service URL. Otherwise, it is possible to set the host and port of the remote MBean Server:

JMX::MBean.establish_connection :host => "localhost", :port => 3000

Attributes & Operations naming convention

JMX attributes are available on the JRuby objects but their names are changed to be snake_cased instead of CamelCased.
For example, the LoadedClassCount attribute of the java.lang:type=ClassLoading MBean is available as the loaded_class_count attribute on the corresponding JRuby object.

Ditto for the operations:

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

The set_logger_level method corresponds to the setLoggerLevel MBean operations.

Features List

For now, the features of jmx4r are:

Next steps are:

2 Responses to “jmx4r, a JMX Libary for JRuby”

  1. Aaron Batalion Says:

    Jeff,

    Great to see you leveraged some of the code from here in jmx4r. I’d like to continuing contributing as well. Can you add me to the google project?

    Thanks!

  2. jmesnil Says:

    Aaron, I added you as a project member.

    Welcome on board!

    –jeff