Receiving a message

 

An application receives messages using a MessageConsumer object. The application creates a MessageConsumer object by using the createConsumer() method on a Session object. This method has a destination parameter that defines where the messages are received from. See Destinations for details of how to create a destination, which is either a Queue or a Topic object.

In the point-to-point domain, the following code creates a MessageConsumer object and then uses the object to receive a message:

MessageConsumer messageConsumer = session.createConsumer(ioQueue);
Message inMessage = messageConsumer.receive(1000);
The parameter on the receive() call is a timeout in milliseconds. This parameter defines how long the method must wait if no message is available immediately. We can omit this parameter; in which case, the call blocks until a suitable message arrives. If you do not want any delay, use the receiveNoWait() method.

The receive() methods return a message of the appropriate type. For example, suppose a text message is put on a queue. When the message is received, the object that is returned is an instance of TextMessage.

To extract the content from the body of the message, it is necessary to cast from the generic Message class (which is the declared return type of the receive() methods) to the more specific subclass, such as TextMessage. If the received message type is not known, we can use the instanceof operator to determine which type it is. It is good practice always to test the message class before casting so that unexpected errors can be handled gracefully.

The following code uses the instanceof operator and shows how to extract the content of a text message:

if (inMessage instanceof TextMessage) {
  String replyString = ((TextMessage) inMessage).getText();
  .
  .
  .
} else {
  // Print error message if Message was not a TextMessage.
  System.out.println("Reply message was not a TextMessage");
}

If an application sends a message within a transaction, the message is not delivered to its destination until the transaction is committed. This means that an application cannot send a message and receive a reply to the message within the same transaction.

JMS provides two types of message consumer:

Nondurable message consumer

A nondurable message consumer receives messages from its chosen destination only if the messages are available while the consumer is active.

In the point-to-point domain, whether a consumer receives messages that are sent while the consumer is inactive depends on how WebSphere MQ is configured to support that consumer. In the publish/subscribe domain, a consumer does not receive messages that are sent while the consumer is inactive.

Durable topic subscriber

A durable topic subscriber receives all the messages sent to a destination, including those sent while the consumer is inactive.
The following sections describe how to create a durable topic subscriber, and how to configure WebSphere MQ and the broker to support either type of message consumer.


uj25100_