Java Reference
In-Depth Information
2. The vendor receives the retailer's order message and sends an order message to the
supplier order topic in one transaction. This JMS transaction uses a single session,
so you can combine a receive from a queue with a send to a topic. Here is the code
that uses the same session to create a consumer for a queue and a producer for a
topic:
Click here to view code image
vendorOrderReceiver = session.createConsumer(vendorOrderQueue);
supplierOrderProducer = ses-
sion.createProducer(supplierOrderTopic);
The following code receives the incoming message, sends an outgoing message,
and commits the session. The message processing has been removed to keep the
sequence simple:
Click here to view code image
inMessage = vendorOrderReceiver.receive();
// Process the incoming message and format the outgoing
// message
...
supplierOrderProducer.send(orderMessage);
...
session.commit();
For simplicity, there are only two suppliers, one for CPUs and one for hard drives.
3. Each supplier receives the order from the order topic, checks its inventory, and then
sends the items ordered to the queue named in the order message's JMSReplyTo
field. If it does not have enough of the item in stock, the supplier sends what it has.
The synchronous receive from the topic and the send to the queue take place in one
JMS transaction.
Click here to view code image
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();
Search WWH ::




Custom Search