Show TOC

 Looking Up Enterprise BeansLocate this document in the navigation structure

Lookup from a J2EE Application
  1. In the client's additional deployment descriptor, declare an EJB reference to the bean that you want to look up:
  2. Use the defined EJB reference name to look up the bean:
    • Example client source code if you look up the remote home interface of a bean with reference name ejb/customer :

      javax.naming.Context ctx = new javax.naming.InitialContext();
      CustomerHome customerHome = (CustomerHome) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("java:comp/env/ejb/customer"), CustomerHome.class);
  • Example client source code if you look up the local home interface of a bean with reference name ejb/local_customer :

    javax.naming.Context ctx = new javax.naming.InitialContext();
    CustomerLocalHome customerLocalHome = (CustomerLocalHome) ctx.lookup("java:comp/env/ejb/local_customer");
  1. If the client and the bean belong to different J2EE applications, declare an application reference in the application-j2ee-engine.xml deployment descriptor of the client application. For more information, see Editing Application References .
Lookup from a Non-J2EE Java Application

Instead of declaring EJB references, use the bean's JNDI name to look up the bean. By default, beans are registered in the JNDI namespace during application startup as <application provider>/<application name>/<EJB name> , where:

  • <application provider> is the name of the application provider as specified in the <provider-name> element in application-j2ee-engine.xml
  • <application name> is the name of the application as specified in the <display-name> element in application.xml
  • <EJB name> is the name of the bean as specified in the <ejb-name> element in ejb-jar.xml.
    1. For how to look up the beans, see the example source code above. Replace the reference names with:
      • If you look up the remote home interface of the bean - the bean's default JNDI name.
      • If you look up the local home interface of the bean - the localejbs/ prefix and the bean's default JNDI name.

      It is also possible (although not recommended) to declare an arbitrary JNDI name for your bean and to use that name instead of the default JNDI name of the bean. For more information, see Specifying Arbitrary JNDI Names for Your Beans .

    2. Obtain the client JAR in order to be able to run the beans from within the client application. For more information, see Getting a Client JAR .