background image

The Vendor Message Listener

<< A Local Transaction Example | Transactions JMS Client Example >>
<< A Local Transaction Example | Transactions JMS Client Example >>

The Vendor Message Listener

receiver = session.createConsumer(orderTopic);
...
inMessage = receiver.receive();
if (inMessage instanceof MapMessage) {
orderMessage = (MapMessage) inMessage;
}
// Process message
MessageProducer producer =
session.createProducer((Queue) orderMessage.getJMSReplyTo());
outMessage = session.createMapMessage();
// Add content to message
producer.send(outMessage);
// Display message contentssession.commit();
4. The vendor receives the replies from the suppliers from its confirmation queue and updates
the state of the order. Messages are processed by an asynchronous message listener; this step
shows the use of JMS transactions with a message listener.
MapMessage component = (MapMessage) message;
...
orderNumber = component.getInt(
"VendorOrderNumber");
Order order = Order.getOrder(orderNumber).processSubOrder(component);
session.commit();
5. When all outstanding replies are processed for a given order, the vendor message listener
sends a message notifying the retailer whether it can fulfill the order.
Queue replyQueue = (Queue) order.order.getJMSReplyTo();
MessageProducer producer = session.createProducer(replyQueue);
MapMessage retailerConfirmMessage = session.createMapMessage();
// Format the message
producer.send(retailerConfirmMessage);
session.commit();
6. The retailer receives the message from the vendor:
inMessage = (MapMessage) orderConfirmReceiver.receive();
Figure 31­10
illustrates these steps.
Creating Robust JMS Applications
The Java EE 5 Tutorial · September 2007
950