NonStop JMS User's Manual (H06.03+, J06.03+, NonStop JMS 3.0+)
NSJMS and JMS Client Applications
NonStop Server for Java Message Service User’s Manual—522356-006
4-9
Priority
Priority
JMS allows message priority to be set by the setPriority() method on the
MessageProducer object and retrieved using the getPriority() method on the
MessageConsumer object. Although NSJMS allows message priority to be set and
retrieved, this priority does not affect message-delivery order.
Receiving Messages
A JMS client uses a MessageConsumer to receive messages from a specified
destination. A message consumer is an object created by a session that is used by
JMS clients to receive messages from queues or topics. The PTP messaging
application uses QueueReceiver as its message consumer, and the Pub/Sub
messaging application uses TopicSubscriber as its message consumer.
Receiving Messages from a Queue
To receive a message from a queue, you create a QueueReceiver using the
createReceiver() method. This method uses a Queue parameter to define where
the messages are received from. This code fragment receives a message from a
queue and then reads back the message:
QueueReceiver receiver = session.createReceiver(queue);
Message message = receiver.receive();
The receive() method without parameters blocks indefinitely unless a timeout
parameter is specified. When specified, this parameter defines how long the
receive() method should wait when no messages are available. No delay occurs if
the receiveNoWait() method is used.
The receive() method returns a message of the same type as was sent to the
queue. For example, if a StreamMessage is sent to a queue, the receive() method
returns an object of type StreamMessage.
To pull the content from the body of the message, you must identify the subclass of the
message contents, such as TextMessage. If you do not know the message-contents
subclass, use instanceof to determine if the contents of a message are of a certain
type. It is good practice always to test the message class before casting, so that
unexpected errors are handled gracefully.