11/22/2007

Calling EJB 3 from JRuby client

Calling EJB 3 from JRuby client is very similar to calling EJB 3 from a java standalone client, with fewer lines of code. In this post, I had a sample showing how to call EJB 3 from a standalone java client. The following JRuby test client uses the same business interface and bean class.
require 'java'
include_class 'javax.naming.InitialContext'

ic = InitialContext.new
foo = ic.lookup("foo.FooRemote")
result = foo.echo("This is foo!")
puts "Calling the EJB 3 : #{foo} returned #{result}"
This is the exact command line how to call an EJB deployed on Glassfish v2:
set CLASSPATH=%GLASSFISH_HOME%\lib\javaee.jar;%GLASSFISH_HOME%\lib\appserv-rt.jar;C:\simple-ejb3\dist\foo-client-view.jar
jruby testEJB.rb
Calling the EJB 3 : foo._FooRemote_Wrapper@e9738564 returned This is foo!
Noteworthy points:

1. My JRuby is ruby 1.8.5 (2007-11-01 rev 4810) [x86-jruby1.0.2]. It only supports setting classpath in environment variable, though I prefer to set classpath via -cp or -classpath.

2. The classpath is different for other application servers. At a minimum, you need to include the JavaEE API jar, typically named javaee.jar, and jars for application server naming service and other resources. To invoke more complex EJBs even in glassfish, you may need to include more glassfish jars.

3. No need to use a Properties to instantiate the initialContext. It saves us a couple of lines of code in both JRuby and Java clients.

Technorati Tags: , ,