O'Reilly logo

Java Message Service by Richard Monson-Haefel, David A Chappell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Examining a Queue

A QueueBrowser is a specialized object that allows you to peek ahead at pending messages on a Queue without actually consuming them. This feature is unique to point-to-point messaging. Queue browsing can be useful for monitoring the contents of a queue from an administration tool, or for browsing through multiple messages to locate a message that is more important than the one that is at the head of the queue. The latter scenario is what we chose to explore in our new version the wholesaler application, the QWBrowser .

QWBrowser sends out price quotes to multiple instances of QRetailer, which respond with "Buy" orders. To make this more interesting, we'll modify the retailer so that each order requests a random number of items. The QWBrowser may not be able to fulfill all of the orders from its on-hand inventory.

In order to sell as many items from inventory as possible without going into backorder, QWBrowser examines all of the responses that are pending in the queue, then finds the one order with a quantity that most closely fits the amount it has in inventory. It then synchronously consumes all of the messages using QueueReceiver.receive(long timeout), fulfills the desired order, and places the rest of them in a back-order status.

If you would like to see this in action, shut down all the other wholesalers and retailers you may have running. Start one instance of QWBrowser and four or more instances of QRetailer, each in a separate command window:

java chap5.B2B.QWBrowser ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required