/***************************************************************************/ /* */ /* (c) Copyright IBM Corp. 2004 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: corrid */ /* */ /* Description: Sample C++ program that shows the use of the correlation */ /* id in both puts and gets of messages. */ /* */ /***************************************************************************/ /* */ /* Function: */ /* */ /* This program shows the use of the ImqMessageTracker object to set */ /* the correlation id for both the use of puts and gets of messages to */ /* a queue. It has three correlation ids that it uses to put and get */ /* the messages. The specific id is selected by checking for a 1 or 2 in */ /* the user input of the message to put on the queue. If a 1 or 2 is */ /* found in the message, then the correlation id with the 1 or 2 is used. */ /* Anything else results in the correlation id with the 3 being used. */ /* The same logic is used to determine which correlation id should be used */ /* to retrieve messages from the queue. */ /* */ /* This program has been tested with the MSVC++ 6.0 compiler: */ /* */ /* cl -MT corrid.cpp imqb23vn.lib imqs23vn.lib */ /* */ /* Note: Use imqc23vn.lib instead of imqs23vn.lib for a client connect. */ /* */ /***************************************************************************/ /* */ /* corrid 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; ImqGetMessageOptions gmo; ImqPutMessageOptions pmo; MQLONG buflen; /* buffer length */ char buffer[MAX_BUFF_SIZE]; /* used for message on puts/gets */ char buffer2[MAX_BUFF_SIZE]; /* used for correlation id */ MQLONG qCC; cout << "Sample corrid C++ start" << endl; if (argc < 2) { cout << "Required parameter missing - queue name" << endl; exit(99); } /***************************************************************/ /* 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 both */ /* input and output. */ /*************************************************************/ queue.setConnectionReference( qmgr ); queue.setName( argv[ 1 ] ); queue.setOpenOptions( MQOO_INPUT_AS_Q_DEF + MQOO_OUTPUT + MQOO_INQUIRE + MQOO_FAIL_IF_QUIESCING ); 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; } cout << "Enter messages, blank line to end" << endl << " Use a 1 or 2 in message text to specify correlation id to use;" << endl << " If neither specified, then correlation id 3 is used" << endl; qCC = queue.completionCode( ); while ( qCC != 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.useEmptyBuffer(buffer, sizeof(buffer)); msg.setFormat(MQFMT_STRING); msg.setMessageLength(buflen); /*******************************************************/ /* Look at the message to see if it contains a 1 or 2. */ /* If not, the third correlation id is used. */ /*******************************************************/ if ( strstr(buffer, "1") ) { strcpy(buffer2, "111111111111111111111111"); } else if ( strstr(buffer, "2") ) { strcpy(buffer2, "222222222222222222222222"); } else { strcpy(buffer2, "333333333333333333333333"); } ImqBinary corrid(buffer2, 24); msg.setCorrelationId(corrid); if (!queue.put(msg, pmo)) { cout << "ImqQueue::put ended with reason code " << queue.reasonCode( ) << endl; } qCC = queue.completionCode( ); } else { break; /* empty line ... */ } } /* end while */ /************************************************************/ /* All of the messages have been put to the queue. Now ask */ /* for the correlation id to use on each of the gets. */ /************************************************************/ while ( qCC != MQCC_FAILED ) { cout << "Enter correlation id to use on get (1,2,3)" << endl << " blank line to end" << endl; cin.getline(buffer2, MAX_BUFF_SIZE); buflen = strlen(buffer2); /************************************************************/ /* See if a correlation id was entered. If so, perform the */ /* get. If not, exit. */ /************************************************************/ if (buflen > 0) { msg.useEmptyBuffer( buffer, sizeof( buffer ) - 1 ); /* buffer size available for GET */ gmo.setOptions( MQGMO_WAIT | /* wait for new messages */ MQGMO_FAIL_IF_QUIESCING ); gmo.setWaitInterval( 2000 ); /* 2 second limit for waiting */ while ( qCC != MQCC_FAILED ) { /**************************************************************/ /* Set message id to null. The user input will be used as */ /* the correlation id. */ /**************************************************************/ msg.setMessageId( ); /*******************************************************/ /* Look at the message to see if it contains a 1 or 2. */ /* If not, the third correlation id is used. */ /*******************************************************/ if ( strstr(buffer2, "1") ) { strcpy(buffer2, "111111111111111111111111"); } else if ( strstr(buffer2, "2") ) { strcpy(buffer2, "222222222222222222222222"); } else { strcpy(buffer2, "333333333333333333333333"); } ImqBinary corrid(buffer2, 24); msg.setCorrelationId(corrid); if ( queue.get( msg, gmo ) ) { /**************************************************************/ /* Display each message received */ /**************************************************************/ if ( msg.formatIs( MQFMT_STRING ) ) { buffer[ msg.dataLength( ) ] = 0 ; /* add terminator */ cout << "message <" << msg.bufferPointer( ) << ">" << endl; } else { cout << "Non-text message" << endl; } } else { /* report reason, if any */ if ( queue.reasonCode( ) == MQRC_NO_MSG_AVAILABLE ) { /* special report for normal end */ cout << "No more messages" << endl; } else { /* general report for other reasons */ cout << "ImqQueue::get ended with reason code" << (long)queue.reasonCode( ) << endl; /* treat truncated message as a failure for this sample */ if ( queue.reasonCode( ) == MQRC_TRUNCATED_MSG_FAILED ) { break ; } } qCC = queue.completionCode( ); } } /* end of get loop */ qCC = MQCC_OK; } else { /* nothing entered, exit */ break; } } /****************************************************************/ /* Close the target queue (if it was opened) */ /****************************************************************/ if ( !queue.close()) { cout << "ImqQueue::close ended with reason code " << queue.reasonCode( ) << endl; } /****************************************************************/ /* Disconnect from MQM if not already connected */ /****************************************************************/ if (!qmgr.disconnect()) { cout << "ImqQueueManager::disconnect ended with reason code " << qmgr.reasonCode( ) << endl; } /******************************************************************/ /* End of CORRID */ /******************************************************************/ cout << "Sample corrid C++ end" << endl; return(0); }