31 Aug 2010
I've recently done a proof of concept using RabbitMQ, Spring AMQP and Spring Integration. This...
21 Jul 2010
In over ten years of experience building software, there are patterns and nuances that you...
13 Jul 2010
I set out to find an elegant solution to providing growl notifications for a JRuby...
05 May 2010
I've started playing around with Heroku lately and ran into an issue when I created...
30 Apr 2010
I've been meaning to investigate something that came to mind a while back and just...

JRuby: Calling Ruby's ActiveRecord from Java Using the Bean Scripting Framework

Published: 25 Jul 2007
JRuby: Calling Ruby’s ActiveRecord from Java Using the Bean Scripting Framework

Here we need to use the Bean Scripting Framework (BSF) since Java 5 doesn’t have the built-in scripting support that Java 6 has. I will also use 2 helper classes (JRubyHelper.java and BSFHelper.java) provided by ociweb.com. For brevity I will leave out the source of the helper classes and concentrate on the actual code to make a call to a Ruby script from Java:

book_titles

Line 31: initializes an empty ArrayList that will be passed into the Ruby script. Line 32: declares (or registers) the books by name which can then be referenced in the Ruby script. Line 33: actually executes the Ruby script. Lines 36-40: just looping over the books and printing the titles.

Here I’ve created an arjava directory under the Rails plugins directory:

arjava_dir

book_titles.rb

is the script called from Java:

book_titles

models.rb

defines the book Ruby object and extends ActiveRecord::Base

models

query.rb

is just a script to test the models.rb dependency.

query

For simplicity and just to prove the call to Ruby’s ActiveRecord will work from Java, I setup an Ant target to test it:

ant_arjava

That’s it for now. For a good write-up on ActiveRecord, Bruce Tate wrote an article on developerWorks: "Crossing borders: Exploring ActiveRecord"


blog comments powered by Disqus