Perform lookup in an EJB or Web container in the same cell

When an application that is running within an EJB or Web container wants to perform a lookup of an EJB in the same cell, then best practice is to use an EJB reference in the application code and an InitialContext object with no parameters.

An EJB reference is a method of delegating the JNDI name resolution of an EJB from the application to the deployment descriptor. Using the prefix java:comp/env/ on the front of the JNDI name informs the container that this particular JNDI lookup resolves to a reference specified in the deployment descriptor. This removes the reliance on either hard coded JNDI names in the application code or reliance on external properties files.

Only when running in either a Web, EJB, or J2EE application client container can references be used as it is the job of the container to resolve those references using the deployment descriptors.

The binding of an EJB reference to a real JNDI name is specified in the deployment descriptor and so can be altered during or after deployment using the WebSphere Administrative Console.

The InitialContext object used to perform the lookup does not need any parameters if the target EJB is in the same cell. Leaving the InitialContext object empty will mean the local appserver's naming service in which the client is running will be used for lookups. This is because all the name spaces throughout the cell are linked, so a fully qualified JNDI name will locate the EJB home.

So, if performing the lookup from within a Web or EJB container to an EJB that is in another process in the same cell, then the JNDI name needs to be either fully qualified with the node and server that contains the EJB or, if the EJB is on a clustered server, the cluster name. This means the JNDI name ejb/myEJB has to become one of the following to work:

cell/nodes/myNode/servers/myServer/ejb/MyEJB
cell/clusters/myCluster/ejb/myEJB

Example 6-1 shows a lookup of an EJB home when the client is running in an EJB or Web container that is looking up an EJB in the same cell. The EJB reference...

java:comp/env/ejb/BeenThere

...has been set to resolve to...

cell/clusters/MyCluster/ejb/BeenThere

...in the EJB deployment descriptor. Example 6-1 Lookup of an EJB home

// Get the initial context

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;

Context initialContext = new InitialContext();

// Look up the home interface using the JNDI name

try 
{
 java.lang.Object ejbHome = 
    initialContext.lookup("java:comp/env/ejb/BeenThere");

 beenThereHome = 
    (BeenThereHome)javax.rmi.PortableRemoteObject.narrow(
    (org.omg.CORBA.Object) ejbHome, BeenThereHome.class);
}

// Error getting the home interface
catch (NamingException e) 
{ 
 ...
}

Delegating the details of both the JNDI name and the InitialContext location to the container makes the application code much more portable between different environments.

In Server cluster with fault-tolerant initial context there is a discussion about fault tolerant name lookups, this does not apply here as if the local naming service is unavailable then the appserver itself will probably not be running.

 

Mapping EJB references to enterprise beans

With IBM WAS Network Deployment V5.x, you can map the EJB references to enterprise beans after deployment of your application. System administrators can bind EJB references specified by the application developer to a required EJB home in a target operational environment by setting the JNDI name value using the Administrative Console as follows:

  1. Go to...

    Applications | Enterprise Applications | <application_name> | Select Map EJB references to beans

  2. Enter the JNDI name of your Enterprise Bean(s) in the JNDI Name column.

  3. Stop and restart all appserver(s) where your application is installed.

Figure 6-3 Mapping EJB references to Enterprise Beans

Mapping of EJB references to Enterprise Beans can also be performed at deployment time of your application. This is done in the step titled "Map EJB references to beans", as shown in Figure 6-4.

Figure 6-4 Deployment time, mapping EJB references to Enterprise Beans

  Prev | Home | Next

 

WebSphere is a trademark of the IBM Corporation in the United States, other countries, or both.

 

IBM is a trademark of the IBM Corporation in the United States, other countries, or both.