Java Reference
In-Depth Information
TABLE 20-2. JMS Message Types
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
}