users@glassfish.java.net

Problem getting reference to EJB from outside of EJB

From: <glassfish_at_javadesktop.org>
Date: Tue, 03 Mar 2009 13:51:01 PST

I'm using NetBeans 6.5 as my IDE. I have two EJB's (OneEjb and TwoEjb) with Local interfaces which are both in the same enterprise application. OneEjb is a very large file. I want OneEjb to make a call on TwoEjb. Since OneEjb is large, I've created a separate class (CallTwo) to handle all interactions with TwoEjb. OneEjb calls a method on CallTwo, which then calls TwoEjb and returns the return value. I use NB's insert code in CallTwo to insert the necessary code (Insert Code | Call Enterprise Bean...). This inserts a lookupTwoEjb method which performs a JNDI lookup on TwoEjb and returns a reference. When I build and deploy the enterprise app, all is well. However, when CallTwo's method tries to call TwoEjb, I get a NameNotBound exception on TwoEjb.

The problem seems to be that TwoEjb isn't registered with the JNDI service in this scenario. If I remove the lookup code in CallTwo and instead use NB's Insert Code | Call Enterprise Bean... to add a reference to TwoEjb in OneEjb, it adds an @EJB annotation and the appropriate variable to OneEjb. I can then pass this reference into CallTwo and all is right with the world (for the most part). This, however, seems like a temporary solution...not what I really want.

Can anyone explain what is going on here and why this is working the way it works? Is there a way to simply access TwoEjb from CallTwo, like I want? I tried adding the @EJB annotation and TwoEjb reference to CallTwo, but, as I expected, it didn't work. It seems like it only works from within a bean.

Thanks
[Message sent by forum member 'dmocek' (dmocek)]

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