Drawing Hands by M.C. Escher (1948)

Jeff Mesnil

Archive for the ‘jruby’ Category


jmx4r 0.0.3, documentation and multiple connections

On July 4th, 2007 in java, jmx, jmx4r, jruby, ruby (1 Comment »)

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

On June 27th, 2007 in java, jmx, jmx4r, jruby, ruby (No Comments »)

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

On June 11th, 2007 in java, jmx, jmx4r, jruby, ruby (2 Comments »)

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

On May 31st, 2007 in java, jmx, jmx4r, jruby, ruby (1 Comment »)

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…)

JMX Scripts using JRuby

On March 23rd, 2007 in java, jmx, jmx4r, jruby, ruby (4 Comments »)

[update: fixed Ruby script by using ManagementFactory::newPlatformMXBeanProxy instead of MBeanServerInvocationHandler::newProxyInstance based on Daniel comment]

I needed to write a script to automate the management of a Java application using JMX which I could put in a crontab.

I found some explanation on how to script JMX in jconsole with BeanShell or with Groovy. It’s a good approach but:

It turns out that it is straightforward to write such scripts using JRuby and Java 5 or later. (more…)