FileDocCategorySizeDatePackage
ReservationProcessorBean.javaAPI DocExample5667Thu Mar 14 16:12:54 GMT 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(MapMessage reservationMsg, TicketDO ticket)


        // create a ticket and send it to the proper destination
		System.out.println("ReservationProcessor::deliverTicket()..");

		Queue queue = (Queue)reservationMsg.getJMSReplyTo();
        System.out.println(queue.getQueueName());
		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 ne) { /* do nothing */ }
    
public 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 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 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(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 java.sql.Date(System.currentTimeMillis()));
                    
            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);
            System.out.println("Created Ticket and Delivering ticket");
            deliverTicket(reservationMsg, ticket);

        } catch(Exception e) {
            e.printStackTrace();
            throw new EJBException(e);
        }
    
public voidsetMessageDrivenContext(javax.ejb.MessageDrivenContext mdc)

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