users@glassfish.java.net

Re: Problem getting reference to EJB from outside of EJB

From: <glassfish_at_javadesktop.org>
Date: Tue, 03 Mar 2009 14:17:40 PST

It works like that because you are using local interface of TwoEjb.
Try your example with remote interface. Lookup in CallTwo will work.

This is because remote interfaces are registered within global JNDI context and are available from any place of JVM,cluster and from any other place that can do lookups.

Local interfaces are not. Local interfaces can only be registered in component's context. When you use @EJB annotation app server does that for you automatically. When you are not using @EJB annotation and for some reason prefer to do lookup on your own (like in your example) you have to add TwoEjb's local interface into OneEjb's jndi context. When you do that code that is executed within OneEjb's methods will be able to successfully lookup TwoEjb (eg CallTwo).

You can do that like this:

@Statefull
@EJB(name="ejb/TwoEjbLocal", beanInterface=TwoEjbLocal.class, beanName="TwoEjb")
public class OneEjb implements OneEjbLocal{
      ........

}

then in CallTwo you will be able to lookup TwoEjb:

new InitialContext().lookup("java:comp/env/ejb/TwoEjbLocal");

or use context injected into OneEjb with JNDI name without "java:comp/env"
[Message sent by forum member 'sasol' (sasol)]

http://forums.java.net/jive/thread.jspa?messageID=334928