views:

259

answers:

1

I'm having problems calling EJB3 stateless bean outside the container.

Code for getting the object reference:

Context envCtx = (Context) context.lookup("ejb");
MyObject o = (MyObject) envCtx.lookup(MyObject);

The second row results in exception:

java.lang.ClassCastException: javax.naming.Reference

I use JBoss.org 5.1.0 GA.

Based on some other posts I suspect this might be due to wrong version of client libraries. However, I'm unsure which library jar(s) I should include in the jar? (I get the error using 5.0.4.GA jnpserver.)

+2  A: 

For JBoss, your code should look something like that:

Properties properties = new Properties();
properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs","=org.jboss.naming:org.jnp.interfaces");
properties.put("java.naming.provider.url","localhost:1099");

Context context = new InitialContext(properties);
(EchoBeanRemote) c.lookup("EchoBean/remote");

If you prefer, you can put the JNDI environement settings in a jndi.properties file (that needs to be on the classpath):

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099

And use the non-arg InitialContext constructor:

Context context = new InitialContext();
(EchoBeanRemote) c.lookup("EchoBean/remote");

This is obviously more portable.

And in both case, you'll need jbossall-client.jar on the classpath on the client side.

P.S.: You can check the Global JNDI Name your bean is registered at in the JNDI View of the web-based JMX console (if it still exists).

Pascal Thivent