Creating a Stateless Session Bean


by jGuru

[Exercise | API Docs | Short Course| Exercises]

Help is available for each task.



    Task 1

    Design a wrapper class called MusicData, that can represent the persistent fields in MusicCD (namely upc, title, artist, type, price).


    Don't forget to implement java.io.Serializable.

    MusicData should provide a constructor for setting its instance fields with the MusicCD attributes:

      ...
      public MusicData(String upc, String title,
                       String artist, String type, float price) {
        this.upc    = upc;
        this.title  = title;
        this.artist = artist;
        this.type   = type;
        this.price  = price;
      }
      ...
    

    Task 2

    Design an Inventory session bean that provides an addInventory() service. This argument should take an array of MusicData objects as an argument. Within the addInventory() method, this bean should establish a MusicCDHome reference and then loop through the data passed as an argument to addInventory(), creating MusicCD beans and setting their attributes.


    InventoryHome should prescribe a single bean-creation method with the signature:

    public Inventory create() throws CreateException, RemoteException;
    

    Inventory should prescribe one method only, addInventory(), with an interface similar to the following, depending on your design:

    public boolean addInventory(MusicData[] data) 
                   throws RemoteException;
    

    Because Inventory is a session bean, it has no primary key and its creation methods have no arguments: create() and ejbCreate().

    Note that InventoryBean.addInventory() is similar to MusicClient in that it must establish a JNDI context, look up the MusicCD bean, and so on.

    Task 3

    Compile all the classes that compose your bean.


    Don't forget that the compiler needs to be able to find the javax.ejb classes, which are in J2EE_HOME/j2ee.jar. You may use the following command to compile:

    javac -classpath %CLASSPATH%;%J2EE_HOME%\lib\j2ee.jar;. musicstore\*.java

    Task 4

    Package your session bean into a jar file, using the provided XML deployment descriptor.


    Execute the command jar cvf Inventory.jar musicstore\*.class META-INF\ejb-jar.xml to create the jar file.

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