Sun Java System Message Queue 4.3 Developer's Guide for Java Clients

Browsing Messages

If the destination from which you are consuming messages is a point-to-point queue, you can use a queue browser to examine the messages in the queue without consuming them. The session method createBrowser creates a browser for a specified queue:

QueueBrowser  myBrowser = mySession.createBrowser(myDest);

The method will throw an exception (InvalidDestinationException) if you try to pass it a topic destination instead of a queue. You can also supply a selector string as an optional second argument:

String        mySelector = "/* Text of selector here */";
QueueBrowser  myBrowser  = mySession.createBrowser(myDest, mySelector);

Table 2–17 shows the methods defined in the QueueBrowser interface. The getQueue and getMessageSelector methods return the browser’s queue and selector string, respectively.

Table 2–17 Queue Browser Methods

Name 

Description 

getQueue

Get queue from which this browser reads 

getMessageSelector

Get message selector 

getEnumeration

Get enumeration of all messages in the queue 

close

Close browser 

The most important queue browser method is getEnumeration, which returns a Java enumeration object that you can use to iterate through the messages in the queue, as shown in Example 2–4.


Example 2–4 Browsing a Queue


Enumeration  queueMessages = myBrowser.getEnumeration();
Message      eachMessage;

while ( queueMessages.hasMoreElements() )
  { eachMessage = queueMessages.nextElement();
    /* Do something with the message */
  }
               

The browser’s close method closes it when you’re through with it:

myBrowser.close();