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-11
Retrieving Data from a Message
Creating the consumer for a topic (a durable subscriber), starting a transaction,
receiving the message, and then ending the transaction is shown in this code
fragment:
char *clientID = “MyClientID”;
char *subscriptionName = “MySubName”;
char *topicName = “MyTopic”;
// Creating a Durable Subscriber
Consumer *pConsumer = pSes->createDurableSubscriber(
clientID,
subscriptionName,
topicName);
// Starting Transaction, Receiving the Message
BEGINTRANSACTION();
pMsg = pConsumer->receive(3000);
ENDTRANSACTION();
To terminate a durable subscriber use the Session::unsubscribe() method and
pass in the Consumer pointer and the unique clientID and subName that created
the subscriber.
Terminating a durable subscription is shown in this code fragment:
// Unsubscribe a Durable Subscription
pSes->unsubscribe(clientID, subName);
Durable subscriptions do not terminate automatically when their
Session::freeConsumer() method is called. Only an unsubscribe() operation
ends a durable subscription. Subsequent durable subscriber objects that have the
same identity can resume the subscription in the state it was left by the prior subscriber
by calling createDurableSubscriber() and using a previously used client ID,
subscription name, and topic. If no subscriber is active for a durable subscription,
NSJMS retains the subscription's messages until they are received by the subscription
or until they expire.
Retrieving Data from a Message
The receive() method returns the same type of message that 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 a message, you must identify the subclass of the
message contents, such as TextMessage. If you do not know the subclass of the
message contents, use Message::getJMSType() to determine the subclass before
retrieving a specific subclass of data from a message as shown in the following code
Note. The Consumer receive operation must be wrapped within a transaction because the
MESSAGE table is audited.