background image

JMS Queue Browsers

<< Message Properties | JMS Exception Handling >>
<< Message Properties | JMS Exception Handling >>

JMS Queue Browsers

TABLE 31­2
JMS Message Types
Message Type
Body Contains
TextMessage
A java.lang.String object (for example, the contents of an XML file).
MapMessage
A set of name-value pairs, with names as String objects and values as primitive
types in the Java programming language. The entries can be accessed sequentially
by enumerator or randomly by name. The order of the entries is undefined.
BytesMessage
A stream of uninterpreted bytes. This message type is for literally encoding a body
to match an existing message format.
StreamMessage
A stream of primitive values in the Java programming language, filled and read
sequentially.
ObjectMessage
A Serializable object in the Java programming language.
Message
Nothing. Composed of header fields and properties only. This message type is
useful when a message body is not required.
The JMS API provides methods for creating messages of each type and for filling in their
contents. For example, to create and send a TextMessage, you might use the following
statements:
TextMessage message = session.createTextMessage();
message.setText(msg_text);
// msg_text is a String
producer.send(message);
At the consuming end, a message arrives as a generic Message object and must be cast to the
appropriate message type. You can use one or more getter methods to extract the message
contents. The following code fragment uses the getText method:
Message m = consumer.receive();
if (m instanceof TextMessage) {
TextMessage message = (TextMessage) m;
System.out.println(
"Reading message: " + message.getText());
} else {
// Handle error
}
JMS Queue Browsers
You can create a QueueBrowser object to inspect the messages in a queue. Messages sent to a
queue remain in the queue until the message consumer for that queue consumes them.
Therefore, the JMS API provides an object that allows you to browse the messages in the queue
and display the header values for each message. To create a QueueBrowser object, use the
Session.createBrowser
method. For example:
The JMS API Programming Model
Chapter 31 · The Java Message Service API
909