Local EJB

来源:互联网 发布:淘宝信誉等级怎么看 编辑:程序博客网 时间:2024/05/19 21:01

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 theejb-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 toportablejava:globalnames, there are default portableJNDI namesfortwo other scopes :java:module,and java:app.  An EJB component's java:moduleJNDIname is visible to any code running within thesame module.   An EJB component'sjava: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 theejb-jarfile or .warfile in which the EJB component is defined, minus the fileextension.   The<module-name>can be explicitlyspecified usingthe<module-name>element of theejb-jar.xml(for ejb-jars) orweb.xml(forEJB components defined in .wars). 

<bean-name>corresponds to the session bean'sejb-name. Itdefaults to theunqualifiedname of the session bean class.  Itcan beexplicitly specified using thenameattribute of the@Stateless/@Stateful/@Singletonannotation.  Alternatively, if theejb-jar.xmlis being used to define the component,<bean-name>corresponds to the<ejb-name>element ofejb-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:modulesyntaxvs. 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 interfaceFooLocalfrom 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 andBarBean,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.