|
Home TOC Index |
|
Search
Feedback |
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
deploytoolutility 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 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 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.javasource code illustrates the basic tasks performed by the client of an enterprise bean:Locating the Home Interface
The
ConverterHomeinterface defines life-cycle methods such ascreate. Before theConverterClientcan invoke thecreatemethod, it must locate and instantiate an object whose type isConverterHome. This is a four-step process.
- Create an initial naming context.
Context initial = new InitialContext();
- The
Contextinterface 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
InitialContextobject, which implements theContextinterface, provides the starting point for the resolution of names. All naming operations are relative to a context.- Obtain the environment naming context of the application client.
Context myEnv = (Context)initial.lookup("java:comp/env");
- The
java:comp/envname is bound to the environment naming context of theConverterClientcomponent.- Retrieve the object bound to the name
ejb/SimpleConverter.Object objref = myEnv.lookup("ejb/SimpleConverter");
- The
ejb/SimpleConvertername is bound to an enterprise bean reference, a logical name for the home of an enterprise bean. In this case, theejb/SimpleConvertername refers to theConverterHomeobject. The names of enterprise beans should reside in thejava:com/env/ejbsubcontext.- Narrow the reference to a
ConverterHomeobject.ConverterHome home = (ConverterHome) PortableRemoteObject.narrow(objref, ConverterHome.class);Creating an Enterprise Bean Instance
To create the bean instance, the client invokes the
createmethod on theConverterHomeobject. Thecreatemethod returns an object whose type isConverter. The remoteConverterinterface defines the business methods of the bean that the client may call. When the client invokes thecreatemethod, the EJB container instantiates the bean and then invokes theConverterBean.ejbCreatemethod. The client invokes thecreatemethod as follows:Converter currencyConverter = home.create();Invoking a Business Method
Calling a business method is easy--you simply invoke the method on the
Converterobject. The EJB container will invoke the corresponding method on theConverterEJBinstance that is running on the server. The client invokes thedollarToYenbusiness 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
ConverterClientprogram follows.import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import java.math.BigDecimal; 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(); 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.earfileDuring the packaging process you can view the deployment descriptor by selecting Tools
Descriptor Viewer.
To start the New Application Client wizard, select File
New
Application Client. The wizard displays the following dialog boxes.
- Introduction dialog box
- JAR File Contents dialog box
- In the combo box, select
ConverterApp.- Click Edit.
- In the tree under Available Files, locate the
j2eetutorial/examples/build/ejb/converterdirectory.- Select the
ConverterClient.classfile and click Add.- Click OK.
- Click Next.
- General dialog box
Specifying the Application Client's Enterprise Bean Reference
When it invokes the
lookupmethod, theConverterClientrefers to the home of an enterprise bean:Object objref = myEnv.lookup("ejb/SimpleConverter");You specify this reference as follows.
- In the tree, select
ConverterClient.- Select the EJB Refs tab.
- Click Add.
- In the Coded Name column, enter
ejb/SimpleConverter.- In the Type column, select Session.
- In the Interfaces column, select Remote.
- In the Home Interface column, enter
ConverterHome.- In the Local/Remote Interface column, enter
Converter.
|
Home TOC Index |
|
Search
Feedback |