Cannot call EJBs 3.0 from the JRuby script

Hello guys,

I am following the instructions in the book JRuby on Rails in the
“chapter 9 - A JRuby enterpise Bean” and I am getting a strange error.

I have glassfish correctly installed and working. I am using Eclipse
Java EE perpective to create the bean and deploy it to the server. The
Bean compiles and is deployed with no issues to glassfish.

I have created the little shell (called runjr.sh) at the end of the
page 179 adding the glassfish JARs to the classpath as follows:

#!/bin/sh
GLASSFISH=/usr/local/java_ee
DOMAIN=${GLASSFISH}/domains/domain1
export CLASSPATH=${CLASSPATH}:${GLASSFISH}/lib/appserv-rt.jar
export CLASSPATH=${CLASSPATH}:${GLASSFISH}/lib/j2ee.jar
export CLASSPATH=${CLASSPATH}:${GLASSFISH}/lib/j2ee-svc.jar
export CLASSPATH=${CLASSPATH}:${GLASSFISH}/lib/javaee.jar
export CLASSPATH=${CLASSPATH}:${DOMAIN}/applications/j2ee-modules/
JRubyEJB/com/bb/BBServiceRemote.class
export CLASSPATH=${CLASSPATH}:${DOMAIN}/applications/j2ee-modules/
JRubyEJB/com/bb/BBService.class
jruby $*

I have created the JRuby script described in page 180 as follows:

require ‘java’
import javax.naming.InitialContext
ic = InitialContext.new
x = ic.lookup(“com.bb.BBServiceRemote”)
x.invoke(“admin”,“admin”,“create”,“seq1”)
puts x.invoke(“admin”,“admin”,“next”,“seq1”)
puts x.invoke(“admin”,“admin”,“next”,“seq1”)
puts x.invoke(“admin”,“admin”,“next”,“seq1”)

When I run the script (runjr.sh bbservicecall.rb) I receive this error:

com/sun/ejb/EJBUtils.java:425:in `lookupRemote30BusinessObject’:
javax.naming.NamingException: ejb ref resolution error for remote
business interfacecom.bb.BBServiceRemote (NativeException)

I have read some post about this error and some of them describes that
the issue is that the client application (the JRuby script in this
case), tries to resolve the Interface in its own scope instead of it
remotely. So they suggest to explicitely include the interface and the
bean in the classpath, which I did. But it still does not work. Any
suggestions? I work with Mac OS X 10.5.6 and jruby 1.1.5.

Thanks in advance,

GA

Sorry if I am sending this email twice. But I think it did not went
through.

Thanks,

GA

Begin forwarded message:

Guillermo A. wrote:

Hello guys,

I am following the instructions in the book JRuby on Rails in the
“chapter 9 - A JRuby enterpise Bean” and I am getting a strange error.

require ‘java’
import javax.naming.InitialContext
ic = InitialContext.new
x = ic.lookup(“com.bb.BBServiceRemote”)
x.invoke(“admin”,“admin”,“create”,“seq1”)
puts x.invoke(“admin”,“admin”,“next”,“seq1”)
puts x.invoke(“admin”,“admin”,“next”,“seq1”)
puts x.invoke(“admin”,“admin”,“next”,“seq1”)

Using the default InitialContext constructor I believe will only attempt
to look in the current process for JNDI resources. You need to provide
properties to it to connect to the remote server’s JNDI, something like
this:

import java.lang.Properties
properties = Properties.new
properties.put javax.naming.Context::PROVIDER_URL,
“IIOP://remote_host_ip_address:2809/”
properties.put javax.naming.Context::INITIAL_CONTEXT_FACTORY,
“com.ibm.websphere.naming.WsnInitialContextFactory”
context = InitialContext.new(properties)

Someone more familiar with GlassFish (or you yourself) can probably find
the appropriate property values for GlassFish (these are some WebSphere
properties I found quickly googling for “jndi remote”).

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hello guys,

I’ve solved the problem.

The issue was the classpath. I was pointing the classpath to the
“.class” directly in the glassfish server. But for some reason Jruby
was not finding the classes. I have created a .jar including the
classes inside and then I pointed the classpath to the location of the
jar file and it worked just fine.

Cheers,

GA


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Guillermo A. wrote:

Hello guys,

I’ve solved the problem.

The issue was the classpath. I was pointing the classpath to the
“.class” directly in the glassfish server. But for some reason Jruby was
not finding the classes. I have created a .jar including the classes
inside and then I pointed the classpath to the location of the jar file
and it worked just fine.

Ahh, yes, that’s one complaint I’ve always had about CLASSPATH myself;
it needs to point at something that contains the classes, either a dir
or a jar file. Glad to hear you’ve got it working now.

If you could, adding something to the wiki might help others that run
into this issue connecting to an EJB Server.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hello Charles,

the jndi.properties file is present. It is inside of one of the jar
files included in the classpath: appserv-rt.jar.

The server is supposed to find the file inside of the menitoned jar
file.

I have created the same code that I am testing in Ruby but in Java and
it works perfectly. It connects and it executes the Bean without
problems. But from jRuby nothing happens, just the error:

com/sun/ejb/EJBUtils.java:425:in `lookupRemote30BusinessObject’:
javax.naming.NamingException: ejb ref resolution error for remote
business interfacecom.bb.BBService (NativeException)

The Ruby code that does not work is:

require ‘java’

import javax.naming.InitialContext

ic = InitialContext.new
x = ic.lookup(“com.bb.BBService”)
x.invoke(“admin”,“admin”,“create”,“seq1”)
puts x.invoke(“admin”,“admin”,“next”,“seq1”)
puts x.invoke(“admin”,“admin”,“next”,“seq1”)
puts x.invoke(“admin”,“admin”,“next”,“seq1”)

And the java code that works is:

import javax.naming.InitialContext;
import com.bb.*;

public class Main {

public static void main(String[] args) {

try {
  InitialContext ic = new InitialContext();
  BBService bb = (BBService) ic.lookup("com.bb.BBService");
  bb.invoke("admin", "admin", "create", "seq1");
  System.out.println(bb.invoke("admin", "admin", "next", "seq1"));
  System.out.println(bb.invoke("admin", "admin", "next", "seq1"));
  System.out.println(bb.invoke("admin", "admin", "next", "seq1"));
} catch (Exception ex) {
  System.err.println("Caught an unexpected exception!");
  ex.printStackTrace();
}

}

public Main() {
super();
}

}

Any ideas?

GA