The J2EETM Tutorial
Home
TOC
PREV TOC NEXT Search
Feedback

Creating the J2EE Application Client

A J2EE application client is a program written in the Java programming language. At run time, the client program executes in a different virtual machine (VM) than the J2EE server.

The J2EE application client in this example requires two different JAR files. The first JAR file is for the J2EE component of the client. This JAR file contains the client's deployment descriptor and its class files. When you run the New Application Client wizard, the deploytool automatically creates the JAR file and stores it in the application's EAR file. Defined by the J2EE Specifications, the JAR file is portable across all compliant J2EE servers.

The second JAR file contains stub classes that are required by the client program at run time. These stub classes enable the client to access the enterprise beans that are running in the J2EE server. Because this second JAR file is not covered by the J2EE Specifications, it is implementation-specific, intended only for the J2EE SDK.

The J2EE application client source code is in examples/src/ejb/converter/ConverterClient.java. You already compiled this code along with the enterprise bean code in the section, Compiling the Source Files.

Coding the J2EE Application Client

The ConverterClient.java source code illustrates the basic tasks performed by the client of an enterprise bean:

Locating the Home Interface

The ConverterHome interface defines life-cycle methods such as create. Before the ConverterClient can invoke the create method, it must instantiate an object whose type is ConverterHome. This is a three-step process:

  1. Create a JNDI naming context.
     Context initial = new InitialContext(); 
    
  2. Retrieve the object bound to the name ejb/SimpleConverter.
    Object objref = initial.lookup
       ("java:comp/env/ejb/SimpleConverter"); 
    
  3. Narrow the reference to a ConverterHome object.
     ConverterHome home =
        (ConverterHome) PortableRemoteObject.narrow(objref,
        ConverterHome.class); 
    

Creating an Enterprise Bean Instance

To create the ConverterEJB class, the client invokes the create method on the ConverterHome object. The create method returns an object whose type is Converter. The remote Converter interface defines the business methods in ConverterEJB that the client may call. When the client invokes the create method, the EJB container instantiates ConverterEJB, and then invokes the ConverterEJB.ejbCreate method. The client invokes the create method as follows:

Converter currencyConverter = home.create(); 

Invoking a Business Method

Calling a business method is easy-you simply invoke the method on the Converter object. The EJB container will invoke the corresponding method on the ConverterEJB instance that is running on the server. The client invokes the dollarToYen business method in the following line of code.

double amount = currencyConverter.dollarToYen(100.00); 

ConverterClient Source Code

The full source code for the ConverterClient program follows.

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import Converter;
import ConverterHome;

public class ConverterClient {

   public static void main(String[] args) {

       try {
           Context initial = new InitialContext();
           Object objref = initial.lookup
              ("java:comp/env/ejb/SimpleConverter");
           ConverterHome home = 
               (ConverterHome)PortableRemoteObject.narrow(
				objref, ConverterHome.class);
           Converter currencyConverter = home.create();
 
           double amount = 
              currencyConverter.dollarToYen(100.00);
           System.out.println(String.valueOf(amount));
           amount = currencyConverter.yenToEuro(100.00);
           System.out.println(String.valueOf(amount));

           currencyConverter.remove();

       } catch (Exception ex) {
           System.err.println("Caught an unexpected exception!");
           ex.printStackTrace();
       }
   } 
}  

Compiling the Application Client

The application client files are compiled at the same time as the enterprise bean files as described in Compiling the Source Files.

Packaging the J2EE Application Client

To package an application client component, you run the New Application Client Wizard of the deploytool. During this process, the wizard puts the client files into a JAR file and then adds the JAR file to the application's ConverterApp.ear file.

To start the New Application Client Wizard, select File->New Application Client. The wizard displays the following dialog boxes.

  1. Introduction Dialog Box:
    1. Read this explanatory text for an overview of the wizard's features.
    2. Click Next.
  2. JAR File Contents Dialog Box
    1. Click the Add button next to the Contents text area.
    2. In the tree under Available Files, locate the examples/build/ejb/converter directory.
    3. Select ConverterClient.class and click Add.
    4. Click OK.
  3. General Dialog Box:
    1. In the Main Class combo box, select ConverterClient.
    2. Verify that the entry in the Display Name field is ConverterClient.
    3. In the Callback Handler Class combo box, select container-managed authentication.
    4. Click Finish.

Specifying the Application Client's Enterprise Bean Reference

When it invokes the lookup method, the ConverterClient refers to an enterprise bean:

Object objref = initial.lookup
   ("java:comp/env/ejb/SimpleConverter"); 

You specify this reference as follows:

  1. In the tree, select ConverterClient.
  2. Select the EJB Ref's tab.
  3. Click Add.
  4. In the Coded Name column enter ejb/SimpleConverter.
  5. In the Type column, select Session.
  6. In the Home column enter ConverterHome.
  7. In the Remote column enter Converter.
Home
TOC
PREV TOC NEXT Search
Feedback