Java Reference
In-Depth Information
Destination type is queue
To end program, type Q or q, then <return>
Reading message: This is message 1 from producer
Reading message: This is message 2 from producer
Reading message: This is message 3 from producer
Message is not a TextMessage
7. Type
Q
or
q
to stop the client.
A Simple Example of Browsing Messages in a Queue
This section describes an example that creates a
QueueBrowser
object to examine mes-
sages on a queue, as described in “
JMS Queue Browsers
” on page
358
.
This section then
explains how to compile, package, and run the example using the GlassFish Server.
Writing the Client for the
QueueBrowser
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
Enu-
meration
object. You can then iterate through the
Enumeration
object and display
the contents of each message.
The
messagebrowser/src/java/MessageBrowser.java
client 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:
if ( !msgs.hasMoreElements() ) {
System.out.println("No messages in queue");
} else {
while (msgs.hasMoreElements()) {