Browsing Messages
Client applications can use a QueueBrowser to look at messages spooled for a queue without consuming them.
- The delivery restrictions imposed by a queue’s access type (that is, exclusive or non-exclusive) only affect consuming messages from a queue. They do not affect browsing messages on a queue.
- When the client is using Direct Transport mode, a QueueBrowser cannot be used to browse a temporary queue.
Related Samples
For an example of how to browse messages on a queue, refer to the SolJMSQueueBrowser
sample.
To browse messages on a queue, do the following:
- Create a
QueueBrowser
object.Call
Session.createBrowser(...)
and pass in the queue that you want to browse.Optionally, you can also pass in a selector string for the
Selector
property. Using a selector enables the client to only browse messages that match a selector. Note that it could take longer for the message bus to evaluate spooled messages against a selector, especially if the selector used is complex. For more information on selectors, refer to Selectors. - Use the
QueueBrowser
to get anEnumeration
object that can be used to browse the current queue messages in the order they were received (from oldest to newest) by the queue.Call
QueueBrowser.getEnumeration()
. - Iterate over the messages on the queue.
Call
Enumeration.hasMoreElements()
. This method returnstrue
if there is at least one message available in the browser’s local message buffer; otherwise, it returnsfalse
.If it returns
false
, it does not necessarily mean that the queue is empty, rather the local buffer does not currently contain any messages. Subsequent calls toEnumeration.hasMoreElements()
orEnumeration.nextElement()
could returntrue
and return a message, respectively.If you want to browse every message on a queue, it is recommended that you use
enumeration.nextElement()
. This method keeps returning messages until the local queue is empty and it has not received a message for 10 seconds.
Client applications should not make any assumptions about the polling strategy used by the JMS API when browsing spooled messages. For example, when browsing messages, the JMS API could be requesting more messages from the event broker or returning already buffered messages.
Displaying Messages
To display the contents of a message in a human‑readable form, you can call SolJmsUtility.dumpMessage(Message Msg)
and pass in the message instance. This method returns a text string that includes JMS message fields and their values along with Solace message fields and their values.
This method is provided as an aid for programmers when developing applications.