background image

Browsing Messages in a Queue

<< The Terminal Window | Compiling the MessageBrowser Client >>
<< The Terminal Window | Compiling the MessageBrowser Client >>

Browsing Messages in a Queue

5. Run the AsynchConsumer program:
appclient -client asynchconsumer.jar queue
The output of the program looks like this:
Destination type is queue
To end program, type Q or q, then <return>
Reading message: This is message 1
Reading message: This is message 2
Reading message: This is message 3
Message is not a TextMessage
6. Type Q or q to stop the program.
A Simple Example of Browsing Messages in a Queue
This section describes an example that creates a QueueBrowser object to examine messages on a
queue, as described in
"JMS Queue Browsers" on page 909
. This section then explains how to
compile, package, and run the example using the Application Server.
The following sections describe the steps in creating and running the example:
"Writing the Client Program for the Queue Browser Example" on page 926
"Compiling and Packaging the MessageBrowser Client" on page 927
"Running the Clients for the Queue Browser Example" on page 928
Writing the Client Program for the Queue Browser Example
To create a QueueBrowser for a queue, you call the Session.createBrowser method with the
queue as the argument. You obtain the messages in the queue as an Enumeration object. You
can then iterate through the Enumeration object and display the contents of each message.
The messagebrowser/src/java/MessageBrowser.java program performs the following steps:
1. Injects resources for a connection factory and a queue.
2. Creates a Connection and a Session.
3. Creates a QueueBrowser:
QueueBrowser browser = session.createBrowser(queue);
4. Retrieves the Enumeration that contains the messages:
Enumeration msgs = browser.getEnumeration();
5. Verifies that the Enumeration contains messages, then displays the contents of the messages:
Writing Simple JMS Client Applications
The Java EE 5 Tutorial · September 2007
926