Queue storageOrderQueue = null;
Queue vendorConfirmationQueue = null;
MessageConsumer vendorOrderMessageConsumer = null;
MessageProducer monitorOrderMessageProducer = null;
MessageProducer storageOrderMessageProducer = null;
MapMessage orderMessage = null;
MessageConsumer vendorConfirmationMessageConsumer = null;
VendorMessageListener listener = null;
Message inMessage = null;
MapMessage vendorOrderMessage = null;
Message endOfMessageStream = null;
Order order = null;
int quantity = 0;
try {
connectionFactory =
SampleUtilities.getConnectionFactory();
connection =
connectionFactory.createConnection();
session = connection.createSession(true, 0);
asyncSession = connection.createSession(true, 0);
vendorOrderQueue =
SampleUtilities.getQueue(vendorOrderQueueName, session);
monitorOrderQueue =
SampleUtilities.getQueue(monitorOrderQueueName, session);
storageOrderQueue =
SampleUtilities.getQueue(storageOrderQueueName, session);
vendorConfirmationQueue =
SampleUtilities.getQueue(vendorConfirmationQueueName, session);
} catch (Exception e) {
System.out.println("Connection problem: " + e.toString());
System.out.println("Program assumes six queues named A B C D E F");
if (connection != null) {
try {
connection.close();
} catch (JMSException ee) {}
}
System.exit(1);
}
try {
/*
* Create receiver for vendor order queue, sender for
* supplier order queues, and message to send to suppliers.
*/
vendorOrderMessageConsumer =
session.createConsumer(vendorOrderQueue);
monitorOrderMessageProducer =
session.createProducer(monitorOrderQueue);
storageOrderMessageProducer =
session.createProducer(storageOrderQueue);
orderMessage = session.createMapMessage();
/*
* Configure an asynchronous message listener to process
* supplier replies to inquiries for parts to fill order.
* Start delivery.
*/
vendorConfirmationMessageConsumer =
asyncSession.createConsumer(vendorConfirmationQueue);
listener = new VendorMessageListener(asyncSession, 2);
vendorConfirmationMessageConsumer.setMessageListener(listener);
connection.start();
/*
* Process orders in vendor order queue.
* Use one transaction to receive order from order queue
* and send messages to suppliers' order queues to order
* components to fulfill the order placed with the vendor.
*/
while (true) {
try {
// Receive an order from a retailer.
inMessage = vendorOrderMessageConsumer.receive();
if (inMessage instanceof MapMessage) {
vendorOrderMessage = (MapMessage) inMessage;
} else {
/*
* Message is an end-of-message-stream message from
* retailer. Send similar messages to suppliers,
* then break out of processing loop.
*/
endOfMessageStream = session.createMessage();
endOfMessageStream.setJMSReplyTo(vendorConfirmationQueue);
monitorOrderMessageProducer.send(endOfMessageStream);
storageOrderMessageProducer.send(endOfMessageStream);
session.commit();
break;
}
/*
* A real application would check an inventory database
* and order only the quantities needed. Throw an
* exception every few times to simulate a database
* concurrent-access exception and cause a rollback.
*/
if (rgen.nextInt(3) == throwException) {
throw new JMSException("Simulated database concurrent access exception");
}
// Record retailer order as a pending order.
order = new Order(vendorOrderMessage);
/*
* Set order number and reply queue for outgoing
* message.
*/
orderMessage.setInt("VendorOrderNumber",
order.orderNumber);
orderMessage.setJMSReplyTo(vendorConfirmationQueue);
quantity = vendorOrderMessage.getInt("Quantity");
System.out.println("Vendor: Retailer ordered " +
quantity + " " +
vendorOrderMessage.getString("Item"));
// Send message to monitor supplier.
orderMessage.setString("Item", "Monitor");
orderMessage.setInt("Quantity", quantity);
monitorOrderMessageProducer.send(orderMessage);