NonStop JMS C++ API Programmer's Guide
Using the NSJMS C++ APIs
NonStop Server for Java Message Service C++ API Programmer’s Guide—526459-002
5-10
Receiving a Message
method results in the Consumer destructor being called, which frees all memory used
by the Consumer.
Freeing a consumer is shown in this code fragment:
// Freeing the Consumer
pSes->freeConsumer(pConsumer);
Receiving Messages from a Topic
To receive a message from a topic, clients create a Consumer object using either the
createConsumer() method or the createDurableSubscriber() method from
the Session class, depending on whether a non-durable subscriber or a durable
subscriber is desired.
Non-DurableSubscribers
Non-durable subscribers only receive messages that are published on a chosen topic
while the subscriber is active. For non-durable subscribers, clients create a consumer
object by using the createConsumer() method from the Session class, which has
the Consumer::SUBSCRIBER constant as the consumer type. This method uses a
topic destination to define the location from which the messages are received.
Creating the consumer for a topic (a non-durable subscriber), starting a transaction,
receiving the message, and then ending the transaction is shown in this code
fragment:
char *topicName = “MyTopic”;
// Creating the Subscriber
Consumer *pConsumer = pSes->createConsumer(topicName,
Consumer::SUBSCRIBER);
// Starting Transaction, Receiving the Message
BEGINTRANSACTION();
pMsg = pConsumer->receive(3000);
ENDTRANSACTION();
Non-durable subscribers automatically terminate themselves, that is, the subscription,
when their Session::freeConsumer() method is called.
Durable Subscribers
Durable subscribers receive all messages published on a topic, including messages
published while the subscriber is inactive. For durable subscribers, clients create a
durable subscriber object by using the createDurableSubscriber() method from
the Session class. This method is similar to creating a non-durable subscriber, except
that you pass a client ID and a subscription name to uniquely identify the subscriber.
Note. The Consumer receive operation must be wrapped within a transaction because the
MESSAGE table is audited.