EJB References

EJB components do not exist in a vacuum, and the business logic within EJBs often makes use of other EJBs deployed in the server. Normally, a client references another EJB by looking up the JNDI name where the EJB was deployed. It is undesirable to hard-code the JNDI name within the bean code because this limits the EJB's portability and reusability. One possible solution is to store the JNDI name as an EJB environment entry because this approach allows the names to be changed at deployment time without modifying the bean code. However, there is essentially no validation that can be performed on this JNDI name by the server's tools. For instance, the EJB server cannot validate that this JNDI name corresponds to an EJB of the appropriate type. The EJB specification includes EJB references as a portable way to safely reference another EJB within bean code. Unlike a simple JNDI name, an EJB reference gives the EJB server sufficient information to ensure that the EJB reference corresponds to a deployed EJB of the appropriate type.

Declaring EJB References

Like environment entries, EJB references are defined in the EJB deployment descriptor. The EJB deployer includes the ejb-ref tag to define an EJB reference. The <ejb-ref> tag consists of an optional description, the logical name, the type of the reference, the home interface class, and the remote interface class. The reference type is Session for all session EJBs and Entity for all entity EJBs. (Chapter 9 discusses entity beans in detail.) For example, the following XML tag provides an EJB reference named ejb/WidgetEJB.

<ejb-ref>
  <description>An EJB reference to the Widget EJB</description>
  <ejb-ref-name>ejb/WidgetEJB</ejb-ref-name>
  <ejb-ref-type>Session</ejb-ref-type>
  <home>chapter8.WidgetHome</home>
  <remote>chapter8.Widget</remote>
</ejb-ref>

Each EJB reference also requires an entry in the WebLogic deployment descriptor to map the ejb-ref-name to a concrete JNDI name in the server. The following example maps our ejb-ref-name, ejb/WidgetEJB, to the JNDI name DeployedWidget.

<ejb-reference-description>
  <ejb-ref-name>ejb/WidgetEJB</ejb-ref-name>
  <jndi-name>DeployedWidget</jndi-name>
</ejb-reference-description>

The bean class can now use this EJB reference to refer to the WidgetEJB without storing any concrete JNDI name. Like environment entries, EJB references are stored in JNDI under the java:/comp/env context. It is recommended that EJB references always be stored under the context java:/comp/env/ejb. As you can see in the preceding example, the EJB reference name begins with a leading ejb/ in accordance with this recommendation. The bean code can reference the widget home interface by making a JNDI lookup to java:/comp/env/ejb/WidgetEJB as in the following code:

Context ctx = new InitialContext();
Object h = ctx.lookup("java:/comp/env/ejb/WidgetEJB");
WidgetHome home = (WidgetHome)
   PortableRemoteObject.narrow(h, WidgetHome.class);

The EJB container requires that EJB references specify the concrete JNDI name for each EJB reference in the WebLogic descriptor. This approach is flexible because the EJB reference might point to an EJB in another application that has not yet been deployed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.6.79