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 ...

Get Java Message Service now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.