Local EJB

1、I have an EJB component with aLocalinterface. Can I access it from an Application Client or a stand-alonejava client ?


If the EJB component is running within the server, no.  The EJBLocal view is an optimized invocation path that usescall-by-reference semantics.   It is only available to webcomponents and EJB components that are part of the same applicationas the target EJB component.    To access EJB components thatare running in the server from an Application Client orstand-alone java client,  you'll need to use either a Remote 3.xBusiness interface, a 2.x Home interface, or web services.

One alternative, if using GlassFish v3, is to use the EJB 3.1 Embeddable API. This allows a Java SE program to directly execute EJB components withinthe same JVM, without using a server process. 

2、I have an EJB component with aLocal interface.  Can I access it from a web component in adifferent application?


No.  The EJB specification only requires access to an EJBcomponent's local EJB interface fromwithin the same application in the same JVM.    One option isto package the ejb-jar inthe same .earas the .war.  Asecond option, if using GlassFish v3, is to package the EJB componentdirectly within the .war

3、How do I access a Local EJB componentfrom a POJO?


Local EJB component access is only portable from within the sameapplication, sothePOJO class must be called from a component running within the sameapplicationas the target Local EJB component.  Injection is not supported forPOJO classes,so the POJO needs to look up the local reference. 

If the application is running within GlassFish v3, it can use the portable global JNDInames defined by the EJB 3.1 specification.   In addition toportable java:globalnames, there are default portable JNDI namesfortwo other scopes : java:module,and java:app.  An EJB component's java:module JNDIname is visible to any code running within thesame module.   An EJB component's java:app JNDI nameisvisible to any code running within the same application.  

The syntax for each is :

java:module/<bean-name>

java:app/<module-name>/<bean-name>

<module-name>defaults to the unqualified name of the ejb-jarfile or .warfile in which the EJB component is defined, minus the fileextension.   The <module-name>can be explicitlyspecified usingthe <module-name>element of the ejb-jar.xml(for ejb-jars) or web.xml(forEJB components defined in .wars). 

<bean-name>corresponds to the session bean's ejb-name. Itdefaults to the unqualifiedname of the session bean class.  Itcan beexplicitly specified using the nameattribute of the @Stateless/ @Stateful/ @Singletonannotation.  Alternatively, if the ejb-jar.xmlis being used to define the component, <bean-name>corresponds to the <ejb-name>element of ejb-jar.xml.

So, given :

  @Stateless
 public class FooBean { ... }


A POJO running within the same module as FooBean canlookup an EJBreference to FooBeanusing :

 FooBean foo = (FooBean) newInitialContext().lookup("java:module/FooBean");

An advantage of the java:module syntaxvs. java:globalis that the codedoing the lookup does not have to know the <app-name>or <module-name>in which it is executing. 

If the application is running in GlassFish v2 or earlier, there are noportable JNDInames for the local EJB view.  In that case the onlyway for the POJO to acquire an EJB reference is to define a local EJBdependency using @EJB or ejb-local-ref,and then look up thatdependency within the private namespace( java:comp/env)of the component within whose scope it is executing.  That can be done using the following steps :

Step 1.  Define the local EJB dependency for the componentwithinwhose scope the POJO will execute.


Assume the POJO wants to access a local EJB 3.0 business interface FooLocalfrom the following EJB component defined within the same application :

 @Stateless
 public class FooBean implements FooLocal { ... }


If the POJO is called from a web application, the java:comp/envnamespaceis shared by the entire .war. So, the local EJB dependency can bedefinedby using an @EJBannotation on any managed class within the .war :

 @EJB(name="fooejbref", beanInterface=FooLocal.class)
 public class MyServlet extends HttpServlet { ... }


Likewise, if the POJO is itself called from an EJB component, the localEJBdependencymust be defined within that EJB component.  Unlike the .war case,thecomponent environment ( java:comp/env)is private for each EJB component.

 @EJB(name="fooejbref", beanInterface=FooLocal.class)
 @Stateless
 public class BarBean implements BarLocal { ... }



Alternatively, the local EJB dependency can be declared within thestandarddeployment descriptor corresponding to the component within whose scopethePOJO is executing ( web.xml / ejb-jar.xml)

    <ejb>
      <ejb-name>BarBean</ejb-name>
      <ejb-class>com.acme.BarBean</ejb-class>
 
       ...

        <ejb-local-ref>
         <ejb-ref-name>fooejbref</ejb-ref-name>
         <local>com.acme.FooLocal</local>
         <ejb-link>FooBean</ejb-link>
        </ejb-local-ref>

   </ejb>

Step 2.  In the POJO code, lookup the local EJB dependencywithinjava:comp/env


   InitialContext ic = new InitialContext();

   FooLocal foo = (FooLocal)ic.lookup("java:comp/env/fooejbref");



Note that if the POJO is called from multiple components, e.g. fromboththe web application and BarBean,you'll need to define the same localEJBdependency for both calling components so that it is always defined inthecurrently active component environment when the POJO does its lookup.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值