Prev | Next

TOC | Index

J2EETM Developer's Guide
Clients


Other Enterprise Beans

Although enterprise beans run on the server, one enterprise bean may be the client of another. For example, a session bean is often the client of an entity bean. And the session bean has clients of its own-- perhaps JSP components or Java applications.

The following figure shows the structure of a J2EE application in which one enterprise bean is the client of another. The ShipperClient is a stand-alone Java application that accesses the Shipper session bean. The Shipper session bean is the client of the Stock entity bean, which accesses a database.

FIGURE 7-4 The ShipperApp Sample Application

Setting Up the ShipperApp Application

The sections that follow explain how to assemble the ShipperApp application illustrated in figure 7-4. Before proceeding, you should perform these tasks:

1. Compile the source code files in the doc/guides/ejb/examples/shipper directory:

2. With the Application Deployment Tool, create a J2EE application named ShipperApp.

Creating the Stock EJB .jar File

Invoke the New Enterprise Bean Wizard and step through the following dialog boxes. (You may skip any dialog box not listed here.)

EJB JAR Dialog Box:

a. In the combo box labelled "Enterprise Bean will go in," select ShipperApp.
b. In the JAR Display Name field, enter StockJAR.
c. Add the following files to the Contents text area: Stock.class, StockEJB.class, StockHome.class.
General Dialog Box:

a. In the Enterprise Bean Class combo box, select StockEJB.
b. In the Home Interface combo box, select StockHome.
c. In the Remote Interface combo box, select Stock.
d. Select the Entity radio button.
e. In the Enterprise Bean Display Name field, enter StockBean.
Entity Settings Dialog Box:

Select the radio button labelled "Bean-Managed Persistence."
Resource References Dialog Box:

a. In the Coded Name column enter jdbc/StockDB.
b. In the other columns retain the default values.
Transaction Management Dialog Box:

a. In the Transaction Type column, select Required for the updateOnHand and reorderNeeded methods.
b. Click Finish

Creating the Shipper EJB .jar File

Invoke the New Enterprise Bean Wizard and step through the following dialog boxes. (You may skip any dialog box not listed here.)

EJB JAR Dialog Box:

a. In the combo box labelled "Enterprise Bean will go in," select ShipperApp.
b. In the JAR Display Name field, enter ShipperJAR.
c. Add the following files to the Contents text area: Shipper.class, ShipperEJB.class, ShipperHome.class.
General Dialog Box:

a. In the Enterprise Bean Class combo box, select ShipperEJB.
b. In the Home Interface combo box, select ShipperHome.
c. In the Remote Interface combo box, select Shipper.
d. Select the Session radio button.
e. Select the Stateful radio button.
f. In the Enterprise Bean Display Name field, enter ShipperBean.
Enterprise Bean References Dialog Box:

a. Click Add.
b. In the Coded Name column enter ejb/Stocker.
c. In the Type column select Entity.
d. In the Home column enter StockHome.
e. In the Remote column enter Stock.
f. Click Finish.

Specifying the JNDI Names

In the JNDI Names tabbed pane for the ShipperApp, enter the JNDI names shown in the following table:

TABLE 7-2 ShipperApp JNDI Names

Component/
Reference Name

JNDI Name

ejb/Stocker MyStocker
jdbc/StockDB jdbc/Cloudscape
ShipperBean MyShipper
StockBean MyStocker

The first row in the table links the ejb/Stocker reference to the MyStocker JNDI name. The ShipperEJB class uses ejb/Stocker to reference the StockBean in the following call:

Object objref = initial.lookup("java:comp/env/ejb/Stocker");
The MyStocker JNDI name corresponds to StockBean, the entity bean component for the StockEJB class. This class fetches a DataSource so that it can connect to a Cloudscape database:

private String dbName = "java:comp/env/jdbc/StockDB";
DataSource ds = (DataSource) ic.lookup(dbName);
MyShipper is the JNDI name for ShipperBean, the session bean component for the SessionEJB class. The SessionClient program locates the ShipperBean with the following call:

Object objref = initial.lookup("MyShipper");

Deploying and Running the J2EE Application

1. When you deploy the ShipperApp, select the checkbox labelled "Return Client Jar". In the text field that appears enter the full path name for the ShipperAppClient.jar file.

2. Create the database table used by the ShipperApp:

UNIX:

cd $J2EE_HOME/doc/guides/ejb/examples/shipper
../util/cloudTable.sh
Windows:

cd %J2EE_HOME%\doc\guides\ejb\examples\shipper
..\util\cloudTable
3. To try out the ShipperApp, run the client program:

UNIX:

CPATH=$J2EE_HOME/lib/j2ee.jar:ShipperAppClient.jar:.
java  -classpath "$CPATH" ShipperClient
Windows:

set CPATH=.;%J2EE_HOME%\lib\j2ee.jar;ShipperAppClient.jar
java -classpath "%CPATH%" ShipperClient
4. Although the client program does not print any results, there are two ways you can verify that the ShipperApp ran correctly:

a. If you started the J2EE server with the -verbose option, it will print these lines:

ShipperEJB: ejbCreate()
ShipperEJB: shipItem()
StockEJB: updateOnHand()
StockEJB: reorderNeeded()
b. Run the cloudIJ.sh (UNIX) or cloudIJ.bat (Windows) script in the examples/util directory and query the STOCK table in the database:

ij> select * from stock;
PR&|QUANTITY_ON_HA&|REORDER_LEVEL  
-----------------------------------
123|91             |90             

1 row selected



Prev | Next

TOC | Index


Copyright © 2000 Sun Microsystems, Inc. All rights reserved.