Prev | Next | J2EETM Developer's Guide
Getting Started |
ConverterClient
program is a stand-alone Java application. To create ConverterClient
you perform these steps:
2. Compiling the Client's Code
ConverterClient.java
source code illustrates the basic tasks performed by the client of an enterprise bean:
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.
2. Retrieve the object bound to the nameContext initial = new InitialContext();
MyConverter
. (This is the JNDI name you specified in the section, "Deploying the J2EE Application".)
3. Narrow the reference to ajava.lang.Object objref = initial.lookup("MyConverter");
ConverterHome
object.
ConverterHome home = (ConverterHome) PortableRemoteObject.narrow(objref, ConverterHome.class);
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.
Converter currencyConverter = home.create();
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);
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("MyConverter"); 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(); } } }
compileClient.sh
, change <installation-location> to the directory in which you installed the J2EE SDK.
2. Run the#!/bin/sh J2EE_HOME=<installation-location> CPATH=.:$J2EE_HOME/lib/j2ee.jar javac -classpath "$CPATH" ConverterClient.java
compileClient.sh
script.
compileClient.bat
, change <installation-location> to the directory in which you installed the J2EE SDK.
2. Run theset J2EE_HOME=<installation-location> set CPATH=.;%J2EE_HOME%\lib\j2ee.jar javac -classpath %CPATH% ConverterClient.java
compileClient.ba
t script.