NonStop JMS C++ API Programmer's Guide

NSJMS C++ API Sample Client Applications
NonStop Server for Java Message Service C++ API Programmer’s Guide526459-002
6-6
SimpleReceiver.cpp
// Creating the Consumer.
pConsumer = pSes->createConsumer(destName, Consumer::RECEIVER);
cout << "Receiver 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<< "Rcv Message: " << 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 Receiving "<<msgCount<<" messages, took "<< perf <<
" msecs"<<endl;
pSes->freeConsumer(pConsumer);
}
catch (NSJMS_Exception & excp)
{
cout << "NSJMS_Exception: " << excp.getClassName() << ", " <<
excp.getMessage() << endl;
pSes->freeMessage(pMsg);
pSes->freeConsumer(pConsumer);
return 1;
}
catch(exception & excp)
{
cout << excp.what() << endl;
return 1;
}
return 0;
}
int main(int argc, char* argv[])
{
int optind;
for (optind = 0; optind < argc; optind++)
{
Example 6-2. SimpleReceiver.cpp Sample Client Application (page 2 of 3)