Creating EJB Clients


by jGuru

[Help | API Docs | Short Course| Exercises]

This exercise implements two standalone client applications to use the MusicCD entity bean and Inventory session bean which you developed and deployed in the previous exercises.

Note: Due to a bug in the 1.0 release of the J2EE RI, these client programs will not run properly. The J2EE™ RI does not conform to the J2EE specification regarding the use of compound primary keys. The J2EE RI 1.0 only works properly with single-valued primary keys. In order to make this exercise work with the J2EE RI, you will have to use the upc field of the MusicCDBean class as the primary key and remove all mention of MusicCDPK in the MusicCD entity bean, the Inventory session bean, and the clients in this exercise. This bug will be fixed in the next J2EE RI release.

Prerequisites

Skeleton Code

Tasks


  1. Start the Cloudscape database server with the command-line command cloudscape -start.

  2. Start the J2EE RI via the command-line command j2ee -verbose. Pay attention to the output to make sure that J2EE loads the MusicStoreDB datasource which you set up in the previous exercise, Setting up the Database.

  3. Edit the MusicClient.java skeleton to obtain the JNDI context, and use this context to get a reference to the MusicCDHome.

  4. Create a new MusicCD bean with the UPC code given in the skeleton, and set its fields to the values shown in the skeleton. Or if you have a favorite CD of your own, you can use that data.

  5. Add code to find the MusicCD bean you just created in the database then print out the database values of the fields you set in the previous step.

  6. Compile and run your client application.

  7. Edit the MusicInventoryClient.java skeleton to obtain the JNDI context, and use this context to get a reference to the InventoryHome.

  8. Create a new Inventory bean.

  9. Create an instance of MusicData and fill it with information about CD's you want to insert into the database, then use the Inventory bean's business method to perform this task.

  10. Compile and run your client application.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

As a solution, provided are both the source code for the client applications, and the MusicStoreClient.jar file, which holds the container-generated stubs and skeletons and other things needed for a standalone-client to communicate with the J2EE™ server.

Demonstration

MusicClient produces standard output similar to the following, depending on your choice of data:

myhost> java -classpath %CLASSPATH%;MusicStoreClient.jar; \
        %J2EE_HOME%\lib\j2ee.jar  musicstore.MusicClient
Title: Luna Nueva
Artist: Diego Torres
Type: Latin
Price: 12.58
myhost>

MusicInventoryClient should produce standard output similar to the following:

myhost> java -classpath %CLASSPATH%;MusicStoreClient.jar; \
        %J2EE_HOME%\lib\j2ee.jar  musicstore.MusicInventoryClient
Building new inventory...
Submitting new inventory...
New inventory added to database.
myhost>

Upon examination, the database should then reflect the added inventory. The exact contents of the database will of course depend on what CD information you inserted:

Exercises

Short Course

Copyright 1996-2000 jGuru.com. All Rights Reserved.