Creating EJB Clients


by jGuru

[Exercise | API Docs | Short Course| Exercises]

Help is available for each task.



    Task 1

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


    You may already have Cloudscape running from the previous exercise. If so, there is no need to start it again (in fact, you can't have two copies running). When shutting down Cloudscape, be sure to use cloudscape -stop instead of ^C.

    Task 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.


    Running the J2EE RI via the command-line command j2ee -verbose should produce output similar to the following:

    myhost> j2ee -verbose
    
    Naming service started: :1050
    Published the configuration object ...
    Binding DataSource, name = jdbc/Cloudscape,
        url = jdbc:cloudscape:rmi:CloudscapeDB;create=true
    Binding DataSource, name = jdbc/MusicStore,
        url = jdbc:cloudscape:rmi:MusicStoreDB;create=false
    Configuring web service using "default"
    Web service started: :9191
    Web service started: :7000
    Configuring web service using "default"
    Configuring web service using
        "file:/D:/j2sdkee1.2/public_html/WEB-INF/web.xml"
    Web service started: :8000
    Endpoint [SSL:
        ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=7000]]
        shutdown due to exception:
        javax.net.ssl.SSLException: No available certificate corresponds
            to the SSL cipher suites which are enabled.
    endpoint down: :7000
    Loading jar:/D:/j2sdkee1.2/repository/myhost/applications/
        MusicStore947749297345Server.jar
    /D:/j2sdkee1.2/repository/myhost/applications/
        MusicStore947749297345Server.jar
    Looking up authenticator...
    Binding name:java:comp/env/ejb/MusicCD
    J2EE server startup complete.
    

    Note in particular the lines:

    Binding DataSource, name = jdbc/MusicStore,
        url = jdbc:cloudscape:rmi:MusicStoreDB;create=false
    
    that lets you know that the MusicStore database is known to the J2EE server, and
    Loading jar:/D:/j2sdkee1.2/repository/myhost/applications/
        MusicStore947749297345Server.jar
    /D:/j2sdkee1.2/repository/myhost/applications/
        MusicStore947749297345Server.jar
    Looking up authenticator...
    Binding name:java:comp/env/ejb/MusicCD
    
    that lets you know the MusicStore application has been deployed.

    Task 3

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


    You obtain the JNDI context by creating a new instance of javax.naming.InitialContext and storing it in a reference variable of datatype javax.naming.Context

    Task 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.


    Use the create() method that takes the UPC code as an argument to create the bean. Use the accessor and mutator methods defined in the remote interface to modify the bean's data.

    Task 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.


    Create an instance of MusicCDPK, set the upc field to an appropriate value, and use the findByPrimaryKey() method of the MusicCD home interface to find the MusicCD bean. When you have a reference to the bean, you can read the data using accessor methods on that bean.

    Task 6

    Compile and run your client application.


    Because the client needs to use both the MusicCD bean and the Inventory bean, you will need to put Inventory.jar and MusicCD.jar in your CLASSPATH in order to compile. You will also have to make sure that j2ee.jar is in your CLASSPATH, because that is where the javax.ejb classes are stored.

    Task 7

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


    You obtain the JNDI context by creating a new instance of javax.naming.InitialContext and storing it in a reference variable of datatype javax.naming.Context

    Task 8

    Create a new Inventory bean.


    Use the create() method on the Inventory home interface.

    Task 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.


    The addInventory() method of the Inventory bean takes and instance of MusicData as an argument, and inserts those records into the database.

    Task 10

    Compile and run your client application.


    Because the client needs to use both the MusicCD bean and the Inventory bean, you will need to put Inventory.jar and MusicCD.jar in your CLASSPATH in order to compile. You will also have to make sure that j2ee.jar is in your CLASSPATH, because that is where the javax.ejb classes are stored.

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