NonStop JMS C++ API Programmer's Guide

Using the NSJMS C++ APIs
NonStop Server for Java Message Service C++ API Programmer’s Guide526459-002
5-9
Receiving a Message
Receiving a Message
NSJMS C++ APIs client uses a Consumer to receive messages from a specified
destination. A message consumer is created by passing a destination name (queue or
topic) and consumer type to the createConsumer() method supplied by a Session
class. The PTP messaging application and the Pub/Sub messaging application both
use the Consumer class as its message consumer.
Consumer class
The Consumer class supports a single receive() method:
Message* receive(int timeout);
A client can request the next message from a message consumer that uses its
receive() method. This call blocks until a message arrives or the timeout expires.
The receive() method takes a timeout argument in milliseconds. This value defines
how long the receive() method should wait when there are no messages available.
For example:
receive(3000); //wait 3 seconds
A value of zero blocks the receive() call indefinitely.
Receiving Messages from a Queue
To receive a message from a queue, clients create a Consumer object by using the
createConsumer() method from the Session class, which has the
Consumer::RECEIVER constant as the consumer type. This method uses a queue
destination to define the location from which the messages are received.
Creating the consumer for a queue (a receiver), starting a transaction, receiving the
message, and then ending the transaction is shown in this code fragment:
char *queueName = “MyQueue”;
// Creating the Receiver
Consumer *pConsumer = pSes->createConsumer(queueName,
Consumer::RECEIVER);
// Starting Transaction, Receiving the Message
BEGINTRANSACTION();
Message *pMsg = pConsumer->receive(3000);
ENDTRANSACTION();
Free Consumer Resources
The Session class contains the freeConsumer() method for freeing consumer
resources. This method takes a pointer to a Consumer object as an argument. This
Note. The Consumer receive operation must be wrapped within a transaction because the
MESSAGE table is audited.