FileDocCategorySizeDatePackage
ReservationProcessorBean.javaAPI DocExample6415Sat May 25 23:29:38 BST 2002com.titan.reservationprocessor

ReservationProcessorBean

public class ReservationProcessorBean extends Object implements javax.ejb.MessageDrivenBean, javax.jms.MessageListener

Fields Summary
javax.ejb.MessageDrivenContext
ejbContext
Context
jndiContext
Constructors Summary
Methods Summary
public voiddeliverTicket(javax.jms.MapMessage reservationMsg, com.titan.travelagent.TicketDO ticket)

      
      // create a ticket and send it to the proper destination
      //
      System.out.println ("ReservationProcessor::deliverTicket()..");
      
      Queue queue = (Queue)reservationMsg.getJMSReplyTo ();
      QueueConnectionFactory factory = (QueueConnectionFactory)
      jndiContext.lookup ("java:comp/env/jms/QueueFactory");
      QueueConnection connect = factory.createQueueConnection ();
      QueueSession session = connect.createQueueSession (false,0);
      QueueSender sender = session.createSender (queue);
      ObjectMessage message = session.createObjectMessage ();
      message.setObject (ticket);
      
      System.out.println ("Sending message back to sender..");
      sender.send (message);
      
      connect.close ();
   
public voidejbCreate()

public voidejbRemove()

      try
      {
         jndiContext.close ();
         ejbContext = null;
      } catch(NamingException ignored) { }
   
public com.titan.cabin.CabinLocalgetCabin(java.lang.Integer key)

      // get a local reference to the Cabin EJB
      //
      CabinHomeLocal home = (CabinHomeLocal)
      jndiContext.lookup ("java:comp/env/ejb/CabinHomeLocal");
      CabinLocal cruise = home.findByPrimaryKey (key);
      return cruise;
   
public com.titan.cruise.CruiseLocalgetCruise(java.lang.Integer key)

      // get a local reference to the Cruise EJB
      //
      CruiseHomeLocal home = (CruiseHomeLocal)
      jndiContext.lookup ("java:comp/env/ejb/CruiseHomeLocal");
      CruiseLocal cruise = home.findByPrimaryKey (key);
      return cruise;
   
public com.titan.customer.CustomerRemotegetCustomer(java.lang.Integer key)

      // get a remote reference to the Customer EJB
      //
      Object ref = jndiContext.lookup ("java:comp/env/ejb/CustomerHomeRemote");
      CustomerHomeRemote home = (CustomerHomeRemote)
      PortableRemoteObject.narrow (ref, CustomerHomeRemote.class);
      CustomerRemote customer = (CustomerRemote)home.findByPrimaryKey (key);
      return customer;
   
public voidonMessage(javax.jms.Message message)

      try
      {         
         System.out.println ("ReservationProcessor::onMessage() called..");
         MapMessage reservationMsg = (MapMessage)message;
         
         Integer customerPk = (Integer)
         reservationMsg.getObject ("CustomerID");
         Integer cruisePk =   (Integer)
         reservationMsg.getObject ("CruiseID");
         Integer cabinPk =    (Integer)
         reservationMsg.getObject ("CabinID");
         
         double price = reservationMsg.getDouble ("Price");
         
         String creditCardNum = reservationMsg.getString ("CreditCardNum");
         Date creditCardExpDate = new Date ( reservationMsg.getLong ("CreditCardExpDate") );
         String creditCardType = reservationMsg.getString ("CreditCardType");
         
         CreditCardDO card = new CreditCardDO (creditCardNum,creditCardExpDate,creditCardType);
         
         System.out.println ("Customer ID = "+customerPk+", Cruise ID = "+cruisePk+", Cabin ID = "+cabinPk+", Price = "+price);
         
         CustomerRemote customer = getCustomer (customerPk);
         CruiseLocal cruise = getCruise (cruisePk);
         CabinLocal cabin = getCabin (cabinPk);
         
         ReservationHomeLocal resHome = (ReservationHomeLocal)
         jndiContext.lookup ("java:comp/env/ejb/ReservationHomeLocal");
         
         ReservationLocal reservation =
         resHome.create (customer, cruise, cabin, price, new Date ());
         
         Object ref = jndiContext.lookup ("java:comp/env/ejb/ProcessPaymentHomeRemote");
         
         ProcessPaymentHomeRemote ppHome = (ProcessPaymentHomeRemote)
         PortableRemoteObject.narrow (ref, ProcessPaymentHomeRemote.class);
         
         ProcessPaymentRemote process = ppHome.create ();
         process.byCredit (customer, card, price);
         
         TicketDO ticket = new TicketDO (customer,cruise,cabin,price);
         
         deliverTicket (reservationMsg, ticket);
         
      } 
      catch(Exception e)
      {
         throw new EJBException (e);
      }
   
public voidsetMessageDrivenContext(javax.ejb.MessageDrivenContext mdc)

      ejbContext = mdc;
      try
      {
         jndiContext = new InitialContext ();
      } catch(NamingException ne)
      {
         throw new EJBException (ne);
      }