Jeff Mesnil
Weblog · About

jmx4r 0.0.6 is released

August 1, 2008

jmx4r 0.0.6 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).

This release adds helper methods to make it more natural to work with TabularData attributes and ObjectName properties

Iterate over TabularData attribute

TabularData attributes now behave like regular Ruby Enumerable:

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

runtime = JMX::MBean.find_by_name "java.lang:type=Runtime"
# runtime.system_properties is a TabularData
runtime.system_properties.each do | sysprop | 
  puts "#{sysprop["key"]} = #{sysprop["value"]}"
end

ObjectName properties

ObjectName properties can now be accessed using the [] method:

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

mem_pools = JMX::MBean.find_all_by_name "java.lang:type=MemoryPool,*"
mem_pools.each do |pool|
  # print the 'name' property of the pool's ObjectName
  puts pool.object_name["name"]
end

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

jruby -S gem install jmx4r

and do not hesitate to contribute:

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

Gitweb setup on Mac OS X

July 31, 2008

Update: I have written a new post for an updated setup with a more recent version of Git.

This post is for future references since I had to slightly adapt what is written in the INSTALL file to run Gitweb on my MacBook.

Assuming all my Git projects are in /Users/jmesnil/Work/ and I've already installed Git using MacPorts (sudo port install git-core), the steps to create gitweb.cgi is:

cd ~/Work
# retrieve the latest version of git
git clone git://git.kernel.org/pub/scm/git/git.git
cd git/
make GITWEB_PROJECTROOT="/Users/jmesnil/Work/" \
     GITWEB_CSS="/gitweb/gitweb.css" \
     GITWEB_LOGO="/gitweb/git-logo.png"  \
     GITWEB_FAVICON="/gitweb/git-favicon.png"  \
     bindir=/opt/local/bin
     gitweb/gitweb.cgi

# CGI scripts are located in /Library/WebServer/CGI-Executables
mkdir -p /Library/WebServer/CGI-Executables/gitweb
sudo cp gitweb/gitweb.cgi /Library/WebServer/CGI-Executables/gitweb/

# And the other resources are in /Library/WebServer/Documents/
mkdir -p /Library/WebServer/Documents/gitweb
sudo cp gitweb/gitweb.css gitweb/git-logo.png gitweb/git-favicon.png \   
   /Library/WebServer/Documents/gitweb/

Once everything is copied to the right place, Gitweb is up and running: http://localhost/cgi-bin/gitweb/gitweb.cgi?

Spaces & Expose in Mac OS X 10.5.3

June 10, 2008

As explained by John Gruber, Apple has improved Spaces support in Mac OS X 10.5.3 but there are still some broken features.

When I'm reordering my windows, instinctively, I want to activate Spaces then use Expose All Windows feature to rearrange them. But this does not work: I have to first use Expose then Spaces.

Before 10.5.3, it was not possible to use Expose when Spaces was active. Now, we can activate Spaces and then display All Windows in Expose using the screen corners or the keyboard's function keys:

However it still does not work using mouse keys.
As for Expose Application Windows, it does not work at all when activating Spaces.

The behaviour I'm expecting from Spaces & Expose is to be able to switch between Expose features (All Windows and Application Windows) and Spaces in any order using either screen corners, function keys or mouse keys.

Maybe I'll have this behaviour in Snow Leopard...

Review of JavaScript: The Good Parts

May 27, 2008

I admit: I don't like JavaScript.

I have written and maintained very few scripts and it was a frustrating experience to have them working on several browsers.
However this was many moons ago and I wanted to reevaluate JavaScript after using it in unexpected places (i.e. outside of the browser) like in CouchDB View API or Eclipse Monkey (I already wrote about JMX scripts using Eclipse Monkey).

I was looking for a good book on JavaScript, the language. There are many many books on JavaScript but they focus mainly on JavaScript in the browser and spend thousands pages describing the DOM (please, Messrs. the editors, save the Amazon forest and just print some links to the online DOM documentation...).
I wanted a concise book about writing simple and maintainable code. I also wanted to learn more about the weird syntax constructions spotted when reading non-trivial bits of code, such as processing.js or CouchDB View's map/reduce.

I bought "JavaScript: The Good Parts" on the strength of the author's chapter in "Beautiful Code" and I was not disappointed.
This is exactly the kind of book I'm looking forward to when learning a programming language. It is short (100 pages + 50 pages of appendixes including JSON description) but dense, the sample code are small and meaningful (even the done to death fibonacci and factorial functions used here to explain memoization).
The book does not lose space describing extensively the whole language. It focuses on the subset which is good and proven and do not talk about the edges or the parts which are better forgotten. It really shows how to write code which is both readable, maintainable and elegant.
I'm sure I'll come back to this book every time I read JavaScript code using some peculiarities of the language that I don't understand.

After reading this book, I've got a better understanding of JavaScript and now sees the good (and even beautiful) parts of it. I have a better appreciation for its prototype-based design even if it is hindered by a class-based syntax.
I also find it frustrating that the JavaScript standard library is so useless (no I/O to communicate with the rest of the world). Of course, the library is richer when the code is to run in the browser or on Rhino (gaining access to the whole Java platform) but, by itself, the standard library is very poor compared to what comes bundled with Python or Ruby.

These are critics of the language. For the book, I've got nothing but praises. I recommend it to any programmer wanting to learn more about what is good in JavaScript.

One advice about the code examples: it is much simpler to use Spidermonkey or Rhino than the web browser to run the different scripts and experiment with them interactively.

How to include JavaScript engine in Apple's Java 6 VM

May 14, 2008

After complaining in my previous post, here is a more constructive guide to use JavaScript with Apple's Java 6 VM:

  1. Download JSR 223's engines
  2. Copy jsr223-engines/javascript/build/js-engine.jar to /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/
  3. Download Rhino
  4. Copy rhino1_7R1/js.jar to /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/

You can now use a "JavaScript" engine from Apple's Java 6 VM:

public class JavaScriptTest {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        engine.eval("print('hello, world!')");
    }
}

jrunscript is also working:

    $ cd /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/
    $ ./jrunscript 
    rhino-nonjdk> print("hello, world");
    hello, world
    rhino-nonjdk> 

No JavaScript in Java 6 on Mac OS X

May 14, 2008

I had an idea about using JavaScript from Java 6 and I wanted to give it a try on my MacBook.
No such luck: Apple has recently released Java 6 for Mac OS X Leopard but somehow it does not include Rhino, the Mozilla's JavaScript engine bundled in Sun Java 6 release.

Instead they provided only one engine for AppleScript but frankly:

public class AppleScriptTest {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("AppleScript");
        engine.eval("tell application \"Finder\"\n display dialog \"Who cares?\"\n end tell");
    }
}

Nuff said...

Tribute to Jim Gray

April 26, 2008

Jim Gray

I can't say it better than Pat Helland:

Being at a conference like HPTS was very entertaining as I could look from across the room and see Jim march methodically through all the attendees, many he'd known for years and some he was just meeting. I would smile because it was obvious they were getting the same kindness and curiosity that I so enjoyed. I knew they would benefit from that attention.

That is exactly what I felt when I met Jim Gray at HPTS'03. He made a strong and lasting impression on me with his amazing ability to explain complex things in simple terms.

I met a lot of so-called architects or experts who seem to take pride in being hard to understand (after all, if they could be understood by mere mortals, they wouldn't be experts...)

Jim Gray is the exact opposite:

When strangers approached Jim, he had an uncanny ability to assess what they could understand and the level at which to explain the problem. I've seen him take the most complex issues and dissect them into a framing that allowed a lay person to understand the gist of the problem and then, separately, dig int the deepest and subtlest nuances with another more versed in the topic.

I really feel priviledged to have met Jim Gray.

Monitoring Weblogic 9.2 with JMX and JRuby

April 18, 2008

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.

Shell history meme

April 15, 2008
~$ uname -a
Darwin macbook.local 9.2.2 Darwin Kernel Version 9.2.2: Tue Mar  4 21:17:34 PST 2008; root:xnu-1228.4.31~1/RELEASE_I386 i386 i386
~$ history|awk '{a[$2]++} END{for(i in a){printf "%5d\t%s\n ",a[i],i}}'|sort -rn|head
   132  git
    63  cd
    55  jruby
    54  ant
    42  ls
    30  rake
    14  rm
    13  ./bin/runtest
    12  vim
    11  ps

I guess I'm having fun learning Git with jmx4r.

[Meme via Stefan]

jmx4r 0.0.5 is released with support for custom JMX URL

April 14, 2008

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://:/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