Ruby in Java, Java in Ruby, JRuby or Ruby Java Bridge?

Hosting (J)Rails applications on a high availability Java infrastructure with clusters, loadbalancers and all that shit stuff is great, if you already have it in place.

But does running an app on the “JRE”:http://java.com/de/ make it a “JRuby”:http://jruby.org/ application by default? Do you really want to be stuck on JRuby?

I really like the idea of running code with the most appropriate runtime available, that is why I use “RVM”:http://rvm.beginrescueend.com/ all the time. JRuby is still very slow on startup and tools like “autotest/autospec”:http://www.zenspider.com/ZSS/Products/ZenTest/ take minutes for just a handfull of tests…

So I would like to host Rails applications on a “Tomcat”:http://tomcat.apache.org/, but on the other hand I want the tests to be executed with “MRI”:http://en.wikipedia.org/wiki/Ruby_MRI! This should not be a problem as long as you don’t want to share a common codebase within Ruby and Java.

h2. Integration

Is there a best practice for combining Java and Ruby?

Since every project is different, I think that you’ve got to evaluate the possible solutions to pick the one that fits best.

The next sections cover some approaches on integration of Java and Ruby code. Make sure to “have RVM installed”:http://blog.nofail.de/2010/01/xruby-on-the-mac/ if you want to execute the “provided example code”:http://github.com/phoet/ruby-java-jruby-rjb/. I assume that a JRE is provided with every OS nowadays…

h3. Java in Ruby

There are two common solutions for embeding Java into a Ruby application. The first and obvious one is via JRuby, which can only be run with the JRuby runtime:

require 'java'

puts "access java.util.UUID via JRuby"
puts java.util.UUID.randomUUID().toString()
rvm use jruby 
=> Using jruby 1.5.0.RC1

jruby lib/jruby.rb 
=> access java.util.UUID via JRuby
=> a16fda6a-c57d-43b9-8376-801e48fe56b3

The same thing is possible using the “Ruby Java Bridge (RJB)”:http://rjb.rubyforge.org/ from MRI:

require 'rjb'

puts "access java.util.UUID via RJB"
puts Rjb::import('java.util.UUID').randomUUID().toString()
rvm use 1.8.7
=> Using ruby 1.8.7 p249

ruby lib/rjb.rb 
=> access java.util.UUID via RJB
=> 1db8298c-5486-4933-be00-cdb180388e38

But it is also possible to do it the other way around!

h3. Ruby in Java

Evaluating Ruby code in Java is dead simple with help of the JRuby library. You just need to set up a “scripting container”:http://github.com/phoet/ruby-java-jruby-rjb/blob/master/src/test/java/de/nofail/jruby/JRubyTest.java that executes your scripts:

  @Test
  public void execute_jruby_scriptlet() {
    new ScriptingContainer().runScriptlet("puts 'hello jruby world'");
  }

A more advanced example is to wire a Ruby class as a “Spring bean”:http://springsource.org. You need to provide “some configuration”:http://github.com/phoet/ruby-java-jruby-rjb/blob/master/src/test/resources/applicationContext.xml and “a Java interface”:http://github.com/phoet/ruby-java-jruby-rjb/blob/master/src/test/java/de/nofail/Identifier.java that can be used as the basis for the bean:



    

# identifier.rb
class Identifier
  def setUuid(uuid)
    @uuid = uuid
  end
  def getUuid()
    @uuid
  end
  def to_s()
    "Identifier[#{@uuid}]"
  end
end

# don't forget to return an instance as a bean
Identifier.new
// Identifier.java
public interface Identifier {
  String getUuid();
}
// SpringJRubyBeanTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/applicationContext.xml")
public class SpringJRubyBeanTest {

  @Resource(name = "identifier")
  Identifier identifier;

  @Test
  public void generateUuid_success() {
    System.out.println("uuid: " + identifier);
  }
}

The code is “currently not running with JRuby 1.5.0.RC1”:http://jira.codehaus.org/browse/JRUBY-4737 so you need the latest stable release for testing.

h2. checking Ruby enginges

It is even possible to mix and match all those approches! You just need to keep track of the Ruby engine that is evaluating your code. I created a little helper called “which_ruby”:http://github.com/phoet/which_ruby (available on “rubygems”:http://rubygems.org/gems/which_ruby) as a sidekick for this article, but more on that in the next week.

Some Ruby sugar in your cup of Java!

3 thoughts on “Ruby in Java, Java in Ruby, JRuby or Ruby Java Bridge?

  1. Pingback: Tweets die Ruby in Java, Java in Ruby, JRuby or Ruby Java Bridge? | #nofail erwähnt -- Topsy.com

  2. Pingback: Distinguish Ruby Runtimes with WhichRuby | #nofail

Comments are closed.