/***************************************************************************/ /* */ /* (c) Copyright IBM Corp. 2006 All rights reserved. */ /* */ /* This sample program is owned by International Business Machines */ /* Corporation or one of its subsidiaries ("IBM") and is copyrighted */ /* and licensed, not sold. */ /* */ /* You may copy, modify, and distribute this sample program in any */ /* form without payment to IBM, for any purpose including developing, */ /* using, marketing or distributing programs that include or are */ /* derivative works of the sample program. */ /* */ /* The sample program is provided to you on an "AS IS" basis, without */ /* warranty of any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, */ /* EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ /* Some jurisdictions do not allow for the exclusion or limitation of */ /* implied warranties, so the above limitations or exclusions may not */ /* apply to you. IBM shall not be liable for any damages you suffer as */ /* a result of using, modifying or distributing the sample program or */ /* its derivatives. */ /* */ /***************************************************************************/ /* */ /* Program name: mqsync */ /* */ /* Description: Sample C++ program that shows WebSphere MQ syncpointing of */ /* messages during put requests */ /* */ /***************************************************************************/ /* */ /* Function: */ /* */ /* This program is a modified version of the mqsync.c program that can */ /* be found on the WebSphere MQ sample code section of the web at: */ /* */ /* http://www.developer.ibm.com/tech/samples.html */ /* */ /* This program is a C++ version and shows the use of the C++ objects and */ /* methods to put messages in syncpoint by checking the message buffer */ /* for the following text: */ /* */ /* SYNC or sync: the message is to be put in syncpoint */ /* CMIT or cmit: the syncpointed messages are to be committed */ /* BACK or back: the syncpointed messages are to be backed out */ /* */ /* The text must be in all upper case or all lower case and can be found */ /* any where in the message text. */ /* */ /* If both backout and commit are specified, the backout will be done. */ /* The best way to run this program is in conjunction with the amqsget */ /* sample program. The mqsync program can be run in one window while */ /* the amqsget program can be run in another. The mqsync program can */ /* be run as follows: */ /* */ /* mqsync SYSTEM.DEFAULT.LOCAL.QUEUE */ /* message 1 */ /* message 2 SYNC */ /* message 3 */ /* message 4 sync */ /* message 5 cmit */ /* message 6 sync */ /* message 7 */ /* message 8 back */ /* message 9 */ /* */ /* The amqsget program would then run as follows: */ /* */ /* amqsget SYSTEM.DEFAULT.LOCAL.QUEUE */ /* message 1 */ /* message 3 */ /* message 5 cmit shows up here because not in sync */ /* message 2 SYNC */ /* message 4 sync */ /* message 7 */ /* message 8 back this caused message 6 to be disarded */ /* message 9 */ /* */ /* This program has been tested with the MSVC++ 6.0 compiler: */ /* */ /* cl -MT mqsync.cpp imqb23vn.lib imqs23vn.lib */ /* */ /* Note: Use imqc23vn.lib instead of imqs23vn.lib for a client connect. */ /* */ /* Note 2: This program does NOT use the ImqQueueManager::begin method to */ /* start the unit of work since the queue manager is the only */ /* participant in the unit of work. It could have been used in */ /* this program but is more ideally suited to units of work where */ /* WebSphere MQ is the transaction manager for a global unit of */ /* work. */ /* */ /***************************************************************************/ /* */ /* mqsync has 2 parameters: */ /* queue name (required) */ /* queue manager name (optional) */ /* */ /***************************************************************************/ #include #include #include #include const int MAX_BUFF_SIZE = 100; int main(int argc, char **argv) { ImqQueueManager qmgr; ImqQueue queue; ImqMessage msg; ImqPutMessageOptions pmo; int bSyncPoint = 0; /* indicator if messages are in syncpoint */ MQLONG buflen; /* buffer length */ char buffer[MAX_BUFF_SIZE]; cout << "Sample mqsync C++ start" << endl; if (argc < 2) { cout << "Required parameter missing - queue name" << endl; exit(99); } else { cout << " Use SYNC or sync in message text for syncpoint'ed message" << endl << " Use CMIT or cmit in message text to commit messages" << endl << " Use BACK or back in message text to backout (discard) messages" << endl; } /***************************************************************/ /* Connect to queue manager */ /***************************************************************/ if (argc > 2) { qmgr.setName( argv[ 2 ] ); } if ( !qmgr.connect( ) ) { cout << "ImqQueueManager::connect failed with reason code " << qmgr.reasonCode( ) << endl; exit(99); } /***************************************************************/ /* Get the queue name that will be used and open it for output */ /***************************************************************/ queue.setConnectionReference( qmgr ); queue.setName( argv[ 1 ] ); queue.setOpenOptions( MQOO_OUTPUT | MQOO_INQUIRE ); queue.open(); /***************************************************************/ /* If there was an error opening the queue, print it out. */ /***************************************************************/ if (queue.reasonCode()) { cout << "ImqQueue::open ended with reason code " << queue.reasonCode( ) << endl; } if (queue.completionCode()) { cout << "Unable to open queue for output" << endl; } msg.useEmptyBuffer(buffer, sizeof(buffer)); msg.setFormat(MQFMT_STRING); /****************************************************************/ /* Read lines from the user and put them to the message queue */ /* Loop until null line or there is a failure */ /****************************************************************/ while (queue.completionCode() != MQCC_FAILED) { cin.getline(buffer, MAX_BUFF_SIZE); buflen = strlen(buffer); /****************************************************************/ /* If we got a buffer, put it to the message queue. */ /****************************************************************/ if (buflen > 0) { msg.setMessageLength(buflen); /***************************************************************/ /* See if the message text contains SYNC or sync. If it does, */ /* turn the pmo syncpoint option to on. */ /***************************************************************/ if ( strstr(buffer, "SYNC") || strstr(buffer, "sync")) { pmo.setSyncPointParticipation(TRUE); bSyncPoint = 1; } else { pmo.setSyncPointParticipation(FALSE); } if (!queue.put(msg, pmo)) { cout << "ImqQueue::put ended with reason code " << queue.reasonCode( ) << endl; } else { /*************************************************************/ /* See if the message text indicates to back out or commit */ /* the syncpointed messages. The message has already been */ /* put on the message queue. */ /*************************************************************/ if ( strstr(buffer, "BACK") || strstr(buffer, "back") ) { bSyncPoint = 0; if (!qmgr.backout()) { cout << "ImqQueueManager::backout ended with reason code " << qmgr.reasonCode( ) << endl; } } else if ( strstr(buffer, "CMIT") || strstr(buffer, "cmit") ) { bSyncPoint = 0; if (!qmgr.commit()) { cout << "ImqQueueManager::commit ended with reason code " << qmgr.reasonCode( ) << endl; } } } } else { break; /* empty line ... */ } } /* end while */ /**************************************************************/ /* If the flag that indicates there are messages in syncpoint */ /* is turned on, then commit the messages before exiting. */ /**************************************************************/ if (bSyncPoint) { cout << "Syncpoint not done in loop, performing now" << endl; if (!qmgr.commit()) { cout << "ImqQueueManager::commit ended with reason code " << qmgr.reasonCode( ) << endl; } } /* endif */ /****************************************************************/ /* Close the target queue */ /****************************************************************/ if ( !queue.close()) { cout << "ImqQueue::close ended with reason code " << queue.reasonCode( ) << endl; } /****************************************************************/ /* Disconnect from queue manager */ /****************************************************************/ if (!qmgr.disconnect()) { cout << "ImqQueueManager::disconnect ended with reason code " << qmgr.reasonCode( ) << endl; } /******************************************************************/ /* End of MQSYNC.CPP */ /******************************************************************/ cout << "Sample mqsync C++ end" << endl; return(0); }