Example: Looking up an EJB home or business interface with JNDI

Most applications that use Java™ Naming and Directory Interface (JNDI) run in a container. Some do not. The name used to look up an object depends on whether or not the application is running in a container. Sometimes it is more convenient for an application to use a corbaname URL as the lookup name. Container-based JNDI clients and thin Java clients can use a corbaname URL.

The following examples show how to perform JNDI lookups from different types of applications.

JNDI lookup from an application running in a container

Applications that run in a container can use java: lookup names. Lookup names of this form provide a level of indirection such that the lookup name used to look up an object is not dependent on the object's name as it is bound in the name server's namespace. The deployment descriptors for the application provide the mapping from the java: name and the name server lookup name. The container sets up the java: namespace based on the deployment descriptor information so that the java: name is correctly mapped to the corresponding object.

The following example shows a lookup of an EJB 3.0 remote business interface. The actual home lookup name is determined by the interface's ibm-ejb-jar-bnd.xml binding file, if present, or by the default name assigned by the EJB container if no binding file is present. For more information, see topics on default bindings for business interfaces and homes and on user-defined bindings for EJB business interfaces and homes.
// Get the initial context as shown in a previous example.
...
// Look up the business interface using the JNDI name.
try {
   java.lang.Object ejbBusIntf =
      initialContext.lookup(
         "java:comp/env/com/mycompany/accounting/Account");
   accountIntf =
      (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class);
}
   catch (NamingException e) { // Error getting the business interface
   ...
}

The following example shows a lookup of an EJB 1.x or 2.x EJB home. The actual home lookup name is determined by the application's deployment descriptors. The enterprise bean (EJB) resides in an EJB container, which provides an interface between the bean and the application server on which it resides.

// Get the initial context as shown in a previous example
...
// Look up the home interface using the JNDI name
try {
   java.lang.Object ejbHome =
      initialContext.lookup(
         "java:comp/env/com/mycompany/accounting/AccountEJB");
   accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
      (org.omg.CORBA.Object) ejbHome, AccountHome.class);
}
   catch (NamingException e) { // Error getting the home interface
   ...
}

JNDI lookup from an application that does not run in a container

Applications that do not run in a container cannot use java: lookup names because it is the container which sets the java: namespace up for the application. Instead, an application of this type must look the object up directly from the name server. Each application server contains a name server. System artifacts such as EJB homes are bound relative to the server root context in that name server. The various name servers are federated by means of a system namespace structure. The recommended way to look up objects on different servers is to qualify the name so that the name resolves from any initial context in the cell. If a relative name is used, the initial context must be the same server root context as the one under which the object is bound. The form of the qualified name depends on whether the qualified name is a topology-based name or a fixed name. Examples of each form of qualified name follow.

  • Topology-based qualified names

    Topology-based qualified names traverse through the system namespace to the server root context under which the target object is bound. A topology-based qualified name resolves from any initial context in the cell.

    The topology-based qualified name depends on whether the object resides on a single server or server cluster. Examples of each lookup follow.

    Single server
    The following example shows a lookup of an EJB business interface that is running in the single server, MyServer, configured in the node, Node1.
    // Get the initial context as shown in a previous example.
    // Using the following form of lookup name, it does not matter which
    // server in the cell is used to obtain the initial context.
    ...
    // Look up the business interface using the JNDI name
    try {
       java.lang.Object ejbBusIntf = initialContext.lookup(
          "cell/nodes/Node1/servers/MyServer/com/mycompany/accounting/Account");
       accountIntf =
         (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class);
    }
    catch (NamingException e) { // Error getting the business interface
       ...
    }
    

    The following example shows a lookup of an EJB home that is running in the single server, MyServer, configured in the node, Node1.

    // Get the initial context as shown in a previous example
    // Using the following form of lookup name, it doesn't matter which
    // server in the cell is used to obtain the initial context.
    ...
    // Look up the home interface using the JNDI name
    try {
       java.lang.Object ejbHome = initialContext.lookup(
          "cell/nodes/Node1/servers/MyServer/com/mycompany/accounting/AccountEJB");
       accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
          (org.omg.CORBA.Object) ejbHome, AccountHome.class);
    }
    catch (NamingException e) { // Error getting the home interface
       ...
    }
    Server cluster
    The following example shows a lookup of an EJB business interface which is running in the cluster, MyCluster. The name can be resolved if any of the cluster members is running.
    // Get the initial context as shown in a previous example.
    // Using the following form of lookup name, it does not matter which
    // server in the cell is used to obtain the initial context.
    ...
       // Look up the business interface using the JNDI name
    try {
       java.lang.Object ejbBusIntf = initialContext.lookup(
          "cell/clusters/MyCluster/com/mycompany/accounting/Account");
       accountIntf =
         (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class);
    }
    catch (NamingException e) { // Error getting the business interface
       ...
    }
    

    The following example shows a lookup of an EJB home which is running in the cluster, MyCluster. The name can be resolved if any of the cluster members is running.

    // Get the initial context as shown in a previous example
    // Using the following form of lookup name, it does not matter which
    // server in the cell is used to obtain the initial context.
    ...
       // Look up the home interface using the JNDI name
    try {
       java.lang.Object ejbHome = initialContext.lookup(
          "cell/clusters/MyCluster/com/mycompany/accounting/AccountEJB");
       accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
          (org.omg.CORBA.Object) ejbHome, AccountHome.class);
    }
    catch (NamingException e) { // Error getting the home interface
       ...
    }
  • Fixed qualified names

    If the target object has a cell-scoped fixed name defined for it, you can use its qualified form instead of the topology-based qualified name. Even though the topology-based name works, the fixed name does not change with the specific cell topology or with the movement of the target object to a different server.

    An example lookup of an EJB business interface with a qualified fixed name follows.
    // Get the initial context as shown in a previous example.
    // Using the following form of lookup name, it does not matter which
    // server in the cell is used to obtain the initial context.
    ...
    // Look up the business interface using the JNDI name
    try {
       java.lang.Object ejbBusIntf = initialContext.lookup(
         "cell/persistent/com/mycompany/accounting/Account");
       accountIntf =
         (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class);
       }
    catch (NamingException e) { // Error getting the business interface
    ... 
    
    }

    An example lookup with a qualified fixed name follows:

    // Get the initial context as shown in a previous example
    // Using the following form of lookup name, it doesn't matter which
    // server in the cell is used to obtain the initial context.
    ...
    // Look up the home interface using the JNDI name
    try {
       java.lang.Object ejbHome = initialContext.lookup(
         "cell/persistent/com/mycompany/accounting/AccountEJB");
       accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
          (org.omg.CORBA.Object) ejbHome, AccountHome.class);
       }
    catch (NamingException e) { // Error getting the home interface
    ...
    }

JNDI lookup with a corbaname URL

A corbaname can be useful at times as a lookup name. If, for example, the target object is not a member of the federated namespace and cannot be located with a qualifiied name, a corbaname can be a convenient way to look up the object.

A lookup of an EJB business interface with a corbaname URL follows.
// Get the initial context as shown in a previous example. 
... 
// Look up the business interface using a corbaname URL. 
try { 
   java.lang.Object ejbBusIntf = initialContext.lookup(
      "corbaname:iiop:someHost:2809#com/mycompany/accounting/Account"); 
   accountIntf =
     (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class); 
} 
catch (NamingException e) { // Error getting the business interface 
   ... 
} 

A lookup with a corbaname URL follows.

// Get the initial context as shown in a previous example 
... 
// Look up the home interface using a corbaname URL 
try { 
   java.lang.Object ejbHome = initialContext.lookup(
      "corbaname:iiop:someHost:2809#com/mycompany/accounting/AccountEJB"); 
   accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow( 
      (org.omg.CORBA.Object) ejbHome, AccountHome.class); 
} 
catch (NamingException e) { // Error getting the home interface 
   ... 
}