Jeff Mesnil

JRuby Cookbook Has a Section About jmx4r

I noticed today that O’Reilly just released JRuby Cookbook. I have a few ideas I’d like to implement using JRuby and I was browsing the table of contents to check if the book could be helpful.

I was pleasantly surprised to see that it contains a section about “Performing Remote Management with JMX” using jmx4r (you can read a preview of the section by expanding it from the table of contents).

I’m obviously biased but I deeply believe that a small library such as jmx4r (less than 200 SLOC for the main file and 1/3 are comments) shows what the combination of Ruby and Java can achieve.
JRuby leverages the strong Java runtime (with its garbage collection and hotspot) and allows to access a wide range of Java libraries with all the strengths of the Ruby language.

For example, in jmx4r case, I extensively use Ruby metaprogramming toolset to dynamically create the properties and methods correponding to the MBean attributes and operations.

There are also other stories which demonstrates what JRuby brings to the table coming from the C-Ruby world.

I’m looking forward to read this cookbook and write some ruby code built on top of the Java platform.

Monitoring Weblogic 9.2 with JMX and JRuby

From Tim Koopmans:

After getting nowhere with lack luster HP support, I turned to the power of the Open Source community and got a very simple script up and running to remotely monitor Weblogic JVM Performance and JMS queues using JMX and JRuby.

[...]

This script will enumerate JVM performance and also JMS queue depths in around 50 lines of code

That’s a good example of the conciseness that JRuby brings to the Java platform: in 50 lines of code, Tim connects to a remote Weblogic MBean server, retrieves attributes about memory usage and JMS queue and stores them in a CSV file.

jmx4r 0.0.5 is released with support for custom JMX URL

jmx4r 0.0.5 has just been released (jmx4r is a JRuby library which makes it super easy to write simple Ruby scripts to manage Java applications using JMX).

There is only one enhancement to this release but it is an important one: you can now specify a custom JMX URL to connect to a MBean Server.

Before this release, the URL was hard-wired to connect using the JMX URL defined by Sun service:jmx:rmi:///jndi/rmi://#{host}:#{port}/jmxrmi.

This means it was not possible to use jmx4r to connect to a MBean server which used another URL or another connector that RMI/JRMP.

With this release, you can now fully specify the url:

    url = "service:jmx:rmi:///jndi/iiop://node1:7001/weblogic.management.mbeanservers.runtime"
    JMX::MBean.establish_connection :url => url

As an example, the code above can be used to connect to a Weblogic server using RMI/IIOP.

When the :url argument is used, :hostand :port arguments are ignored. If you’re connecting to a Sun JRE, it is still simpler to specify only :host & :port though.

This enhancement was proposed by Tim Koopmans. Thanks Tim!

As usual, to get this new release, just update the rubygem:

jruby -S gem install jmx4r

jmx4r moved to git

I use Subversion for my daily work (and CVS before that) but I’ve never used a distributed VCS before. One of my ex-colleagues, Marc, explained to me all the advantages of these systems but I never took the time to play with them.
With all the increasing noise about Git and Mercurial, I’m now curious to learn more about it.

Since I learn better by practicing, I moved one of my little projects, jmx4r, from Subversion to Git and hosted it on GitHub.

I don’t use jmx4r at the moment and I don’t plan to develop it more (less features is the new black). However, if you have requests for a new feature or enhancement (or bugs), do not hesitate to fill an issue on the tracker.
Or better, clone the Git project:

git clone git://github.com/jmesnil/jmx4r.git

and start hacking it!

P.S.: With the recent release of JRuby 1.1, I also checked that the latest version of jmx4r had no regression.
Congratulation to the JRuby team for the performance boost since version 1.0!

jmx4r 0.0.4 is released

jmx4r 0.0.4 has just been released.
jmx4r is a JRuby library which makes it super easy to write simple Ruby scripts to manage Java applications using JMX.

To get this new release, just update the rubygem: jruby -S gem install jmx4r

All contributions to this new release were done by Skaar:

  • CompositeData behave like regular read-only Ruby Hash
  • custom classes can be loaded by `require` statements
  • custom JMX credentials are supported

(more…)

Tomcat management using jmx4r

Something which is not obvious with the way jmx4r leverages JMX API and Ruby metaprogramming is that you can write simple scripts to manage a Java application without any dependency on the MBeans exposed by the application.

For simplicity, in my examples I always use MBeans exposed by the JVM but jmx4r works with any MBean even if its interface is unknown from the JVM running the management scripts.

Here is a simple example to manage Tomcat using jmx4r.

Let’s assume that we have Tomcat running locally and manageable remotely on port 3000 (without authentication):

$ export CATALINA_OPTS="-Dcom.sun.management.jmxremote \
    -Dcom.sun.management.jmxremote.port=3000 \
    -Dcom.sun.management.jmxremote.ssl=false \
    -Dcom.sun.management.jmxremote.authenticate=false"
$ ./bin/catalina.sh/run

We want to know among all the Web Modules running in Tomcat which ones are privileged and which ones are not. The whole script to do so is:

# tomcat_modules.rb
require "rubygems"
require "jmx4r"

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

web_modules = JMX::MBean.find_all_by_name "Catalina:j2eeType=WebModule,*"
privileged, unprivileged = web_modules.partition { |m| m.privileged }

puts "Privileged:\n"     + privileged.map   {|m| m.path }.join("\n  ")
puts "Unprivileged:\n  " + unprivileged.map {|m| m.path }.join("\n  ")

Executing this script gives:

$ jruby tomcat_modules.rb 
Privileged:
  /balancer
  /manager
  /host-manager
Unprivileged:
  /tomcat-docs
  /servlets-examples
  /jsp-examples

  /webdav

That’s were using JRuby shines: it combines the simplicity of using directly the MBeans exposed by a Java application without having to bother with classes dependency.

Using Java to write a corresponding script means choosing your “poison”:

  • No class dependency but tedious (and unnatural) use of MBeanServerConnection methods to interact with Tomcat MBeans
  • Use directly Tomcat MBeans (thanks to MBeanServerInvocationHandler) but you must take care having all their interfaces (and dependencies) in the class path of the JVM running the script

I can not thank enough both the JMX and JRuby guys which make it so simple to get the best of both worlds in jmx4r:

  • simple to use directly the MBeans API
  • simple to deploy without class dependency issues

jmx4r 0.0.3, documentation and multiple connections

jmx4r 0.0.3 has just been released.
jmx4r is a JRuby library which makes it super easy to write simple Ruby scripts to manage Java applications using JMX.

Two new features in this release:

  1. some much-needed documentation
  2. as requested by Brian McCallister, I’ve modified the code so that it is now possible to write a script to manage many Java applications at the same time.

For example, the script to trigger a garbage collection on a cluster of Java applications at the same time (quite a bad idea but a simple one):

port = 1090
hosts = ["node1", "node2", "node3", "node4"]
hosts.each do |h|
    memory = JMX::MBean.find_by_name "java.lang:type=Memory", :host => h, :port => port
    memory.gc
end

Quite simple, isn’t it?

(more…)

Gem for jmx4r + authentication

jmx4r is a library for JRuby to make it super easy to write Ruby scripts to manage remote Java applications through JMX.

Thanks to RubyForge, installing jmx4r is now as simple as typing

jruby -S gem install jmx4r

and its use is straightforward

#!/usr/bin/env jruby
require 'rubygems'
require 'jmx4r'

# optional since by default, jmx4r tries to connect to 
# a JMX Server on localhost which listens to port 3000
JMX::MBean.establish_connection :host => "localhost", :port => 3000

memory = JMX::MBean.find_by_name "java.lang:type=Memory"
# trigger a Garbage Collection
memory.gc

Since my previous post on jmx4r, I’ve added unit tests and some examples to highlight its features. It still needs to be properly documented though…

However, one new feature is worth mentioning: jmx4r now supports connection authentication

JMX::MBean.establish_connection :host => "localhost",
    :username => "jeff", :password => "secret"

If you’re using it, I’m very interested to now what you think about it.
And if you encounter any problem, do not hesitate to submit a bug.

jmx4r, a JMX Libary for JRuby

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?

(more…)

JMX Scripts using JRuby — Part II

update: a more Ruby-esque version using Rails idioms by Aaron Batalion

update: updated Ruby script to use instance_eval and define_method instead of eval based on a poignant explanation of eval-less metaprogramming.

In Part I, I created a JRuby script to manage a Java application using JMX.

In this entry, I’ll explain how to remove the dependency on the MBean proxies by taking advantage of Ruby to dynamically add the MBean attributes to a Ruby object representing the MBean.

(more…)