Chapter 23
892
Browsing a Queue
If a client wishes to examine the contents of a queue without removing messages, it can create a
QueueBrowser object to browse the queue. There are two signatures for the createBrowser()
method. CreateBrowser(Queue queue) creates a browser to browse all messages in a queue.
CreateBrowser(Queue queue, String filter) allows the client to filter the messages that are
browsed. Message filtering is discussed in the "Message Properties" section of this chapter.
Once the client creates a QueueBrowser object, it may obtain an enumeration of the messages by
calling the getEnumeration() method on the QueueBrowser. The following code fragment
demonstrates queue browsing:
//create the queue browser
javax.jms.QueueBrowser browser = session.createBrowser(queue);
//get the enumeration of messages in the queue
Enumeration messages = browser.getEnumeration();
//print out the messages
while(messages.hasMoreElements())
{
javax.jms.TextMessage textMessage =
(javax.jms.TextMessage) messages.nextElement();
System.out.println(textMessage.getText());
}
//remember to close the browser
browser.close();
Parts of a JMS Message
JMS Messages are composed of three parts:
q
The Header contains information for use in message identification and routing. The JMS
specification defines a set of header fields and all messages support these fields.
q
The Properties section contains application-specific, standard JMS and provider-specific
properties. Message consumers can use these properties to filter messages.
q
The Body contains the message. The JMS specification defines several types of message bodies.