InfoQ

News

JMX the Ruby way with jmx4r

Posted by Werner Schuster on Jun 29, 2007 07:00 AM

Community
Java,
Ruby
Topics
JRuby,
Deployment / Datacenter,
Dynamic Languages
Tags
JRuby,
Metaprogramming,
JMX
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"

3 comments

Reply

Ruby to shut down 300 JVMs at once in 0.5 second. by Abhay Bakshi Posted Jun 29, 2007 8:34 AM
The credit by Val Aleksenko Posted Jun 29, 2007 3:51 PM
Thanks by Jeff Mesnil Posted Jun 30, 2007 7:53 AM
  1. Back to top

    Ruby to shut down 300 JVMs at once in 0.5 second.

    Jun 29, 2007 8:34 AM by Abhay Bakshi

    This post is interesting. I was also watching Rich Kilmer the other day. Reference: Rich Kilmer on the Power of Ruby, and I was really intrigued by Ruby's features. I haven't used Ruby so far.. Rich says:

    they wanted to be able to freeze these 300 JVM's at once.
    I had a Ruby process that was running all 300 VM's were underneath it, and I could freeze it in about a half of second. All 300 of them.

  2. Back to top

    The credit

    Jun 29, 2007 3:51 PM by Val Aleksenko

    To be fair, the metaprogramming ideas, like that find_by_name example, had come from Aaron Batalion's post

  3. Back to top

    Thanks

    Jun 30, 2007 7:53 AM by Jeff Mesnil

    Werner, thanks for the article! The real work was already done by JMX and JRuby guys: I just needed to put a little glue to bind everything required to write simple management scripts in Ruby. Val, to be fair, I implemented the metaprogramming parts to glue JRuby with JMX without knowing about Aaron code[1] (he used missing_method for attributes while I went the define_method way for them). However once I saw his post, I took his better syntax to make the code looks like Ruby/Rails[2] rather than Java. [1] http://jmesnil.net/weblog/2007/05/31/jmx-scripts-using-jruby-part-ii/ [2] http://revolutiononrails.blogspot.com/2007/05/ruby-esque-jmx-part-2.html

Exclusive Content

SOA Governance: An Enterprise View

Michael Poulin explains the necessity for SOA governance to ensure an Enterprise SOA's success, relying on concepts from the OASIS SOA Reference Model and Reference Architecture.

Developing Portlets using JSF, Ajax, and Seam (Part 2 of 3)

This article covers setting up a RichFaces portlet using JBoss Portlet Container and JBoss Portlet Bridge, deploying a RichFaces portlet, and RichFaces capabilities.

Scalability Worst Practices

This article discusses scalability worst pratices including The Golden Hammer, Resource Abuse, Big Ball of Mud, Dependency Management, Timeouts, Hero Pattern, Not Automating, and Monitoring.

Do the Hustle

Obie Fernandez shares his experience selling consulting services for both Thoughtworks and Hashrocket and give tips how Ruby developers can work with clients.

Natural Laws of Software Development - Deriving Agile Practices

Jeffries and Hendrickson derive Agile practices from the natural laws of software development. They don't just say "Be Agile!", but they explain why Agile practices make perfect sense.

Jinesh Varia About Amazon Alexa Web Service's Architecture

Jinesh Varia talks about the architecture of one of Amazon's web services called Alexa. Jinesh explains how Amazon has reached scalability, performance and reduced costs for the Alexa service.

"We Suck Less!" Is Not Enough

David Douglas and Robin Dymond discuss about companies adopting Agile, but don't go all the way, resulting in failure and rejection of it, and predictably having a negative impact on Agile's future.

The Development of a New Car at Toyota

Kenji Hiranabe talks about Toyota's development process of a new car. Kenji shares his experience meeting Nobuaki Katayama, former Chief Engineer at Toyota, and the lessons he learned from him.