Download
FAQ History |
API
Search Feedback |
Creating the Application Client
An application client is a program written in the Java programming language. At runtime, the client program executes in a different virtual machine than the Application Server. For detailed information on the
appclient
command-line tool, see the man page athttp://java.sun.com/j2ee/1.4/docs/relnotes/cliref/index.html
.The application client in this example requires two JAR files. The first JAR file is for the J2EE component of the client. This JAR file contains the client's deployment descriptor and class files; it is created when you run the New Application Client wizard. Defined by the J2EE Specification, this JAR file is portable across all compliant application 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 Application Server. The JAR file for the stubs is created by
deploytool
when you deploy the application. Because this JAR file is not covered by the J2EE specification, it is implementation-specific, intended only for the Application Server.The application client source code is in the
ConverterClient.java
file, which is in this directory:You compiled this code along with the enterprise bean code in the section Compiling the Source Files.
Coding the 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 ascreate
andremove
. Before theConverterClient
can invoke thecreate
method, 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
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 theContext
interface, 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/env
name is bound to the environment naming context of theConverterClient
component.- 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, theejb/SimpleConverter
name refers to theConverterHome
object. The names of enterprise beans should reside in thejava:comp/env/ejb
subcontext.- 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 theConverterHome
object. Thecreate
method returns an object whose type isConverter
. The remoteConverter
interface defines the business methods of the bean that the client can call. When the client invokes thecreate
method, the EJB container instantiates the bean and then invokes theConverterBean.ejbCreate
method. The client invokes thecreate
method as follows: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 theConverterBean
instance that is running on the server. The client invokes thedollarToYen
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 myEnv = (Context)initial.lookup("java:comp/env");
Object objref = myEnv.lookup("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 Application Client
To package an application client component, you run the New Application Client wizard of
deploytool
. During this process the wizard performs the following tasks.To start the New Application Client wizard, select FileNewApplication Client. The wizard displays the following dialog boxes.
- Introduction dialog box
- JAR File Contents dialog box
- Select the button labeled Create New AppClient Module in Application.
- In the combo box below this button, select
ConverterApp
.- In the AppClient Display Name field, enter
ConverterClient
.- Click Edit Contents.
- In the tree under Available Files, locate this directory:
<
INSTALL
>/j2eetutorial14/examples/ejb/converter/build/
- Select the
ConverterClient.class
file.- Click Add.
- Click OK.
- Click Next.
- General dialog box
Specifying the Application Client's Enterprise Bean Reference
When it invokes the
lookup
method, theConverterClient
refers to the home of an enterprise bean:You specify this reference in
deploytool
as follows.
- In the tree, select
ConverterClient
.- Select the EJB Ref's tab.
- Click Add.
- In the Coded Name field, enter
ejb/SimpleConverter
.- In the EJB Type field, select Session.
- In the Interfaces field, select Remote.
- In the Home Interface field enter,
converter.ConverterHome
.- In the Local/Remote Interface field, enter
converter.Converter
.- In the JNDI Name field, select
ConverterBean
.- Click OK.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.