ejb@glassfish.java.net

Re: Accessing Local 3.0 Session EJB from a Pojo

From: Kenneth Saks <Kenneth.Saks_at_Sun.COM>
Date: Wed, 26 Jul 2006 13:30:16 -0400

Butash, Bob wrote:

> I have an additional question that I have not found an complete answer
> for. I have a Web application that is packaged with the EJB jar file
> in the same EAR file. I would like to use local beans due to the
> performance gains but am not aware of how to access the Local EJB 3.0
> stateless session beans from my pojo objects.
>
> If it was a web object (servlet or JSF 1.2 backing bean) or another
> container managed class I could use injection (@EJB). But in this
> case it is not so injection is out of the question. For the EJB 2.x
> spec I could configure a local reference to the bean via the web.xml
> file, but that is not applicable to the 3.0 beans.
>
Hi Bob,

Actually, it is. ejb-ref and ejb-local-ref can be used to refer to EJB
3.0 Remote/Local Business interfaces in addition to EJB 2.x
Home/LocalHome interfaces. Likewise, @EJB can be used to refer to all
the same combinations. @EJB essentially describes the same set of
meta-data as ejb-ref/ejb-local-ref, only in annotation form. Any @EJB
can be translated into an equivalent ejb-ref/ejb-local-ref.

To use ejb-local-ref to describe an EJB 3.0 local business interface
dependency, just leave off the <local-home> element and specify the
local business interface in the <local> element. E.g.

 <ejb-local-ref>
     <ejb-ref-name>myejbref</ejb-ref-name>
     <local>com.acme.FooLocalBusiness</local>
 </ejb-local-ref>

Alternatively, you can still benefit from annotations to *declare* the
ejb dependency even if
you plan to use an explicit lookup in a utility class instead of
injection. For that, you would
use a TYPE-level @EJB within some class in the .war that does support
Java EE environment
annotations , such as a servlet.

@EJB(name="myejbref", beanInterface=com.acme.FooLocalBusiness.class)
public class MyServlet extends HttpServlet { ... }

Regardless of whether you declare it using .xml or @EJB, the utility
class would then
be able to look it up as :

  InitialContext ic = new InitialContext();
  FooLocalBusiness fooLocal = (FooLocalBusiness)
ic.lookup("java:comp/env/myejbref");


 --ken


> Am I forced to use Remote Beans in this scenario?
>
> Thanks,
>
> Bob Butash
> + bob.butash_at_eds.com
>
>
>