Java Reference
In-Depth Information
Table 10-2. JMS Message Types
Message Type
Creation Method
The message body contains a stream of primitive values in the Java programming language.
Filled and read sequentially.
Message body contains a set of name/value pairs that are formed from Strings objects and
Java primitives. May be accessed sequentially or randomly by name, and the order of entries is
Message body contains a String object. Able to be used for plain text as well as XML messages.
Message body contains a Serializable Java object.
Message body contains a stream of uninterpreted bytes.
The receiving client of a message is also known as the message consumer. Message consumers can be created
using the standard or the simplified JMS API. Using the standard API, a consumer is created from JMS Session objects
in the same manner that producers are created, that is, by calling the createConsumer method of JMS Session and
passing the destination object from which the consumer will listen for and accept messages. Message consumers
have the ability to consume messages that are waiting within a queue, and they listen indefinitely for new incoming
messages. Let's take a look at an example that uses the standard API for consuming messages. In this example, the
method named receiveMessage is responsible for consuming messages from a specified destination point Queue .
public void receiveMessage() {
boolean stopReceivingMessages = false;
if(connection == null){
try(Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);) {
myQueue = (Queue) getContext().lookup("jms/javaEERecipesQueue");
try (MessageConsumer consumer = session.createConsumer(myQueue)) {
while (!stopReceivingMessages) {
Message inMessage = consumer.receive();
if (inMessage != null) {
if (inMessage instanceof TextMessage) {
String messageStr = ((TextMessage) inMessage).getText();
} else {
setDisplayMessage("Message was of another type");
} else {
stopReceivingMessages = true;
} catch (NamingException | JMSException ex) {
Search WWH ::

Custom Search