Deploying a Session Bean

   

The deployment information for a session bean must identify the associated interfaces and classes, specify a session bean type, and select the transaction management option. It also must declare any EJB reference, environment entries, and transaction attributes. You'll see more about these entries in Chapter 12 and Chapter 15, "Deployment." For now, the relevant portions of a deployment descriptor for the auction house stateless session bean are shown in the following:

 <ejb-jar>    <enterprise-beans>      ...      <session>        <ejb-name>AuctionHouse</ejb-name>        <home>com.que.ejb20.auction.controller.AuctionHouseHome</home>        <remote>com.que.ejb20.auction.controller.AuctionHouse</remote>        <ejb-class>com.que.ejb20.auction.controller.AuctionHouseBean</ejb-class>        <session-type>Stateless</session-type>        <transaction-type>Container</transaction-type>        <ejb-local-ref>          <description>This EJB reference is used to locate an auction          </description>          <ejb-ref-name>ejb/EnglishAuction</ejb-ref-name>          <ejb-ref-type>Entity</ejb-ref-type>          <local-home>com.que.ejb20.auction.model.EnglishAuctionHome</local-home>          <local>com.que.ejb20.auction.model.EnglishAuction</local>        </ejb-local-ref>        <ejb-local-ref>          <description>This EJB reference is used to locate a bidder          </description>          <ejb-ref-name>ejb/Bidder</ejb-ref-name>          <ejb-ref-type>Entity</ejb-ref-type>          <local-home>com.que.ejb20.auction.model.BidderHome</local-home>          <local>com.que.ejb20.auction.model.Bidder</local>        </ejb-local-ref>      </session>      ...    </enterprise-beans>    <assembly-descriptor>      ...      <container-transaction>        <method>          <ejb-name>AuctionHouse</ejb-name>          <method-name>*</method-name>        </method>        <trans-attribute>Required</trans-attribute>      </container-transaction>      ...    </assembly-descriptor>  </ejb-jar> 

For more information on assigning transaction attributes, see " Using Container-Managed Transactions," p. 344 .

Deploying a session bean also requires you to specify any vendor-specific deployment parameters. The following fragment shows an example for WebLogic:

 <?xml version="1.0"?>  <!DOCTYPE weblogic-ejb-jar PUBLIC    '-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB//EN'    'http://www.bea.com/servers/wls600/dtd/weblogic-ejb-jar.dtd'>  <weblogic-ejb-jar>    ...    <weblogic-enterprise-bean>      <ejb-name>AuctionHouse</ejb-name>      <reference-descriptor>        <ejb-local-reference-description>          <ejb-ref-name>ejb/EnglishAuction</ejb-ref-name>          <jndi-name>EnglishAuction</jndi-name>        </ejb-local-reference-description>        <ejb-local-reference-description>          <ejb-ref-name>ejb/Bidder</ejb-ref-name>          <jndi-name>Bidder</jndi-name>        </ejb-local-reference-description>      </reference-descriptor>      <jndi-name>AuctionHouse</jndi-name>    </weblogic-enterprise-bean>    ...  </weblogic-ejb-jar> 

Testing the Auction Session Beans

The complete source for the classes and interfaces referenced by this chapter are included on the CD. Using these source files, you can test the entity and session beans developed for the example. Unlike previous chapters, you no longer need to deploy the entity beans with remote interfaces for testing purposes. Now that the session beans are defined, you can use a Java application as a remote client to them. These session beans then can act as local clients to the entity beans. The first step is to create some sample bidder and item data. Possible SQL statements for this appear in Listing 9.7.

Listing 9.7 Sample Data DDL
 INSERT INTO address (id, AddressLine1, AddressLine2, City, State, ZipCode)    VALUES (1, '123 Main Street', null, 'AnyTown', 'NY', '10101');  INSERT INTO address (id, AddressLine1, AddressLine2, City, State, ZipCode)    VALUES (2, '333 Warehouse Row', null, 'AnyTown', 'NY', '10101');  INSERT INTO address (id, AddressLine1, AddressLine2, City, State, ZipCode)    VALUES (3, '225 North Avenue', null, 'Atlanta', 'GA', '30332');  INSERT INTO bidder (id, FirstName, LastName, EmailAddress, UserName, Password,    BillingAddressId, ShippingAddressId) VALUES (1, 'John', 'Smith',    'jsmith@mydomain.com', 'jsmith', 'smitty', 1, 2);  INSERT INTO bidder (id, FirstName, LastName, EmailAddress, UserName, Password,    BillingAddressId, ShippingAddressId) VALUES (2, 'G', 'Burdell',    'gburdell@mydomain.com', 'gburdell', 'ramblin', 3, 3);  INSERT INTO item (id, Name, Description, ImageURL)    VALUES (1, 'DVD Player', 'A-100 DVD player. New, in the original box.',    'images\items\dvd_a100.jpg'); 

It's now possible to access the AuctionManager bean and create an auction. Listing 9.8 shows a client application that locates the home interface for this session bean, creates an instance of the bean, creates an auction, and assigns the existing item to it.

Listing 9.8 AuctionManagerClient.java “A Test Client for the AuctionManager
 package com.que.ejb20;  import java.sql.Timestamp;  import javax.naming.Context;  import javax.naming.InitialContext;  import javax.naming.NamingException;  import javax.rmi.PortableRemoteObject;  import com.que.ejb20.auction.controller.AuctionManager;  import com.que.ejb20.auction.controller.AuctionManagerHome;  import com.que.ejb20.auction.view.AuctionDetailView;  public class AuctionManagerClient {   public void createAuction() {     try {       // pull initial context factory and provider info from jndi.properties        Context ctx = new InitialContext();        // obtain a reference to the auction manager remote home interface        Object home = ctx.lookup("AuctionManager");        AuctionManagerHome managerHome = (AuctionManagerHome)          PortableRemoteObject.narrow(home, AuctionManagerHome.class);        // define the desired auction information        AuctionDetailView view = new AuctionDetailView();        view.setName("DVD Player Auction");        view.setDescription(         "This auction is a 3 day auction for a new DVD player");        long currentTime = System.currentTimeMillis();        view.setStartDateTime(new Timestamp(currentTime));        view.setScheduledEndDateTime(new Timestamp(currentTime +             3*24*60*60*1000));        view.setStartingBid(new Double(75.00));        view.setMinBidIncrement(new Double(5.00));        view.setReserveAmount(new Double(120.00));        // create the auction        AuctionManager manager = managerHome.create();        Integer auctionId = manager.createAuction(view);        System.out.println("Created auction id: " + auctionId);        // assign the item        Integer itemId = new Integer(1);  // the key used by the sample data        manager.assignItemToAuction(auctionId, itemId, 1);        view = manager.getAuctionDetail(auctionId.intValue());        System.out.println(view);        ctx.close();      }      catch (NamingException ne) {       System.out.println(ne.getMessage());      }      catch (Exception e) {       e.printStackTrace();      }    }    public static void main(String[] args) {     AuctionManagerClient auctionClient = new AuctionManagerClient();      auctionClient.createAuction();    }  } 

With an auction defined, the client defined in Listing 9.9 can be used to submit a bid from each of the two bidders found in the sample data.

Listing 9.9 AuctionHouseClient.java “A Test Client for the AuctionHouse
 package com.que.ejb20;  import java.sql.Timestamp;  import java.util.Iterator;  import java.util.List;  import javax.naming.Context;  import javax.naming.InitialContext;  import javax.naming.NamingException;  import javax.rmi.PortableRemoteObject;  import com.que.ejb20.auction.controller.AuctionHouse;  import com.que.ejb20.auction.controller.AuctionHouseHome;  import com.que.ejb20.auction.view.AuctionDetailView;  public class AuctionHouseClient {   public void submitBids(int auctionId) {     try {       // pull initial context factory and provider info from jndi.properties        Context ctx = new InitialContext();        // obtain a reference to the auction house remote home interface        Object home = ctx.lookup("AuctionHouse");        AuctionHouseHome auctionHouseHome = (AuctionHouseHome)          PortableRemoteObject.narrow(home, AuctionHouseHome.class);        AuctionHouse auctionHouse = auctionHouseHome.create();        // submit a bid from each bidder        auctionHouse.submitBid(110.00, auctionId, 1);        auctionHouse.submitBid(130.00, auctionId, 2);        AuctionDetailView view = auctionHouse.getAuctionDetail(auctionId);        System.out.println(view);        List bids = auctionHouse.getBids(1);        Iterator iter = bids.iterator();        while (iter.hasNext()) {         System.out.println(iter.next());        }        ctx.close();      }      catch (NamingException ne) {       System.out.println(ne.getMessage());      }      catch (Exception e) {       e.printStackTrace();      }    }    public static void main(String[] args) {     AuctionHouseClient auctionClient = new AuctionHouseClient();      // pass whatever auction id the bids are for      auctionClient.submitBids(9);    }  } 


Special Edition Using Enterprise JavaBeans 2.0
Special Edition Using Enterprise JavaBeans 2.0
ISBN: 0789725673
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net