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
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.- 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.
- The
- Obtain the environment naming context of the application client.
Context myEnv = (Context)initial.lookup("java:comp/env");
- Retrieve the object bound to the name
ejb/SimpleConverter
.Object objref = myEnv.lookup("ejb/SimpleConverter");
- 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 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 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.
- 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/converter
directory. - Select the
ConverterClient.class
file and click Add. - Click OK.
- Click Next.
- In the combo box, select
- General dialog box
- In the Main Class combo box, select
ConverterClient
. - Verify that the entry in the Display Name field is
ConverterClient
. - In the Callback Handler Class combo box, select container-managed authentication.
- Click Next.
- Click Finish.
- In the Main Class combo box, select
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.
- 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
.
No comments:
Post a Comment