NonStop JMS C++ API Programmer's Guide
NSJMS C++ API Sample Client Applications
NonStop Server for Java Message Service C++ API Programmer’s Guide—526459-002
6-13
SimpleConsumer.cpp
/* Create a new session using a properties file in the current
working directory*/
/***********************************************
pSes = new Session("nsjms.properties");
***********************************************/
// Creating the Consumer.
if (durable)
{
pConsumer = pSes->createDurableSubscriber(clientId, subName,
destName);
}
else
{
pConsumer = pSes->createConsumer(destName, Consumer::SUBSCRIBER);
}
cout << "Consumer has been set" << endl;
long long start = 0;
for(int count = 0; count < msgCount; )
{
// Starting Transaction, Receiving the Message
BEGINTRANSACTION();
pMsg = pConsumer->receive(3000);
ENDTRANSACTION();
if (pMsg != NULL) {
if (strcmp(pMsg->getJMSType(),”TextMessage”) == 0)
{
char * pText = ((TextMessage * )pMsg)->getText();
cout<<"Received message #"<< count << ": " << pText << endl;
delete[] pText;
pSes->freeMessage(pMsg);
pMsg = NULL;
count++;
}
}
else
{
/* This test can stop only after finishing the intervals
requested.*/
cout << "No message found, 3 sec delay ... " << endl;
}
} //end for
long long end = JULIANTIMESTAMP();
long long perf = (julianToMillis(end) - julianToMillis(start));
cout<<"Finished Consuming "<<msgCount<<" messages, took "<< perf << "
msecs"<<endl;
if (durable) pSes->unsubscribe(clientId, subName);
pSes->freeConsumer(pConsumer);
}
catch (NSJMS_Exception & excp)
{
cout << "NSJMS_Exception: " << excp.getClassName() << ", " <<
excp.getMessage() << endl;
pSes->freeMessage(pMsg);
if (durable) pSes->unsubscribe(clientId, subName);
pSes->freeConsumer(pConsumer);
return 1;
}
Example 6-4. SimpleConsumer.cpp Sample Client Application (page 2 of 3)