Friday, June 15, 2012

J2EE Tutorial-10(Creating the J2EE Application Client)

A J2EE application client is a program written in the Java programming language. At runtime, the client program executes in a different virtual machine 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 utility automatically creates the JAR file and stores it in the application's EAR file. Defined by the J2EE Specification, 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 runtime. 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 Specification, it is implementation specific, intended only for the J2EE SDK.

The J2EE application client source code is in j2eetutorial/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

  • Creating an enterprise bean instance

  • Invoking a business method


Locating the Home Interface


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

  1. Create an initial naming context.

        Context initial = new InitialContext();


    The Context interface is part of the Java Naming and Directory Interface (JNDI). A naming context is a set of name-to-object bindings. A name that is bound within a context is the JNDI name of the object.
    An InitialContext object, which implements the Context interface, provides the starting point for the resolution of names. All naming operations are relative to a context.

  2. Obtain the environment naming context of the application client.

       Context myEnv = (Context)initial.lookup("java:comp/env");


    The java:comp/env name is bound to the environment naming context of the ConverterClient component.

  3. Retrieve the object bound to the name ejb/SimpleConverter.

       Object objref = myEnv.lookup("ejb/SimpleConverter");


    The ejb/SimpleConverter name is bound to an enterprise bean reference, a logical name for the home of an enterprise bean. In this case, the ejb/SimpleConverter name refers to the ConverterHomeobject. The names of enterprise beans should reside in the java:com/env/ejb subcontext.

  4. Narrow the reference to a ConverterHome object.

       ConverterHome home =
    (ConverterHome) PortableRemoteObject.narrow(objref,
    ConverterHome.class);




Creating an Enterprise Bean Instance


To create the bean instance, 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 of the bean that the client may call. When the client invokes the create method, the EJB container instantiates the bean and then invokes the ConverterBean.ejbCreate method. The client invokes the createmethod 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 lines of code.

BigDecimal param = new BigDecimal ("100.00");
BigDecimal amount = currencyConverter.dollarToYen(param);


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 java.math.BigDecimal;

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();

BigDecimal param = new BigDecimal ("100.00");
BigDecimal amount =
currencyConverter.dollarToYen(param);
System.out.println(amount);
amount = currencyConverter.yenToEuro(param);
System.out.println(amount);

System.exit(0); } 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 performs the following tasks.

  • Creates the application client's deployment descriptor

  • Puts the deployment descriptor and client files into a JAR file

  • Adds the JAR file to the application's ConverterApp.ear file


After the packaging process you can view the deployment descriptor by selecting ToolsDescriptor Viewer.

To start the New Application Client wizard, select FileNewApplication Client. The wizard displays the following dialog boxes.

  1. Introduction dialog box

    1. Read the explanatory text for an overview of the wizard's features.

    2. Click Next.



  2. JAR File Contents dialog box

    1. In the combo box, select ConverterApp.

    2. Click Edit.

    3. In the tree under Available Files, locate the j2eetutorial/examples/build/ejb/converter directory.

    4. Select the ConverterClient.class file and click Add.

    5. Click OK.

    6. Click Next.



  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 Next.

    5. Click Finish.




Specifying the Application Client's Enterprise Bean Reference


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

Object objref = myEnv.lookup("ejb/SimpleConverter");


You specify this reference as follows.

  1. In the tree, select ConverterClient.

  2. Select the EJB Refs tab.

  3. Click Add.

  4. In the Coded Name column, enter ejb/SimpleConverter.

  5. In the Type column, select Session.

  6. In the Interfaces column, select Remote.

  7. In the Home Interface column, enter ConverterHome.

  8. In the Local/Remote Interface column, enter Converter.

No comments:

Post a Comment