Calling All EJBs — From Ruby?

After installing and playing with JRuby a few days ago, I started wondering if it would be possible to access an EJB from a JRuby script. After all, the Java code to call an EJB is purely client code, only communicating with an application server over an HTTP-like protocol (HTTP-like in BEA’s case, anyway). At my current project, the OptimalJ team generates all their domain code and delivers business services around that in the form of session EJBs to us, the web frontend team. We’re using BEA WebLogic Portal to code the frontend. Now if we’d have EJB access from JRuby, and Rails on JRuby (which seems to be only a matter of time), I guess we would be able to build a frontend like this in Rails. There’s just the tiny matter of convincing the company’s enterprise architect, who’s already sick of hearing me talk about Ruby and Rails.

Anyway, to the code! To my surprise (after 6 years of experience with EJBs, anything EJB-related working instantly comes as a great surprise) it was just a matter of JRubyfying the Java code that you would write to use an EJB. It just works, there’s nothing more to it. Here’s an example of the magic code:

require 'java'
include_class 'java.util.Properties'
include_class 'javax.naming.Context'
include_class 'javax.naming.InitialContext'
properties =
properties.put(Context::PROVIDER_URL, "t3://localhost:7001")
context =
home = context.lookup("business-ejb-jndi-name")
business_ejb = home.create
result = business_ejb.someBusinessMethod(parameters)

Okay, here’s some small print. The example works on a BEA WebLogic application server. For another JEE application server you’ll need to fill in the correct InitialContextFactory class name. Fill in the JNDI name for the EJB after the lookup call. Also, before running the script, you’ll have to put the right libraries in the CLASSPATH environment variable. For example, for the BEA server I had to include weblogic.jar and the EJB client jar containing the home interface to the EJB. But once you’ve figured that out, it should work; I got it to work on JBoss as well as BEA. Consult your local Java guru if necessary.

I’ve put all this in a JRuby module (to be downloaded here), which enables you to write just this to call your favourite EJB:

require 'rejb'
business_ejb = Rejb::ejb("business-ejb-jndi-name")
result = business_ejb.someBusinessMethod(parameters)

I’ve been feeling very happy over the last few days, even though there’s no Spring in sight. Must be the summer of Ruby that’s coming…

2006-04-13. 4 responses.


  1. Pretty cool. I did something similar to this with Jython a while ago (basically publishing to a JMS Queue) and I was excited too. I should start looking into JRuby, looks like it is making some strides!

  2. Yep. I am a professional J2EE developer, a former Python and a present-day Ruby enthusiast. I have tried both Jython and JRuby (although not with EJBs, just plain stuff) and in my experience JRuby is a clear winner when it comes to the compatibility with the ‘mother’ language. Jython is hardly Python2.5 compatible (maybe not even 2.4) while JRuby provides up-to-date (that is 1.8.3 (4)?) compatibility. In this comparison, i would vote for JRuby.

  3. Hello from the JRuby project! I’m one of the devs, and I’m using JRuby for something very similar right now at work to write EJB unit tests. I may also look at implementing business logic in Ruby…it wouldn’t be difficult. We’re currently working to make JRuby more EJB and J2EE-friendly, so you’re certain to see more of these opportunities. Thanks for the exposure!

  4. […] Calling All EJBs — From Ruby? Ideas on J2EE-Ruby integration The question from the top of every Ror/Java FAQ: It’s boring to scale with Ruby on Rails, says DHH […]