/***************************************************************************/ /* */ /* (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: mqreq */ /* */ /* Description: Sample C++ program that shows the use of the message id */ /* and correlation id in passing messages between a request */ /* and reply message. */ /* */ /* This program should be used in conjunction with the mqsrv */ /* sample program. */ /* */ /* The best way to use the mqreq and mqsrv samples is to */ /* start them in separate MS-DOS Prompt windows. They need */ /* to run against the same queue and queue manager. As data */ /* is entered in the mqreq window, the mqsrv program will get */ /* it and reply back to mqreq. When a blank line is entered */ /* in the mqreq window, the program will end. The mqsrv */ /* program has a 15 second timeout on the message gets. */ /* */ /***************************************************************************/ /* */ /* Function: */ /* */ /* This program shows the use of the ImqMessageTracker object to process */ /* the message id and correlation id between a request message and a */ /* reply message. The mqreq program uses the message id that is returned */ /* on the put as the correlation id on the get. The mqsrv program moves */ /* the message id from the get to the correlation id on the put. */ /* */ /* This program has been tested with the MSVC++ 6.0 compiler and */ /* WebSphere MQ 5.3 CSD7: */ /* */ /* cl -MT mqreq.cpp imqb23vn.lib imqs23vn.lib */ /* */ /* Note: Use imqc23vn.lib instead of imqs23vn.lib for a client connect. */ /* */ /***************************************************************************/ /* */ /* mqreq 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 qServer; ImqQueue qReply; ImqMessage msg; ImqMessage msgReply; ImqGetMessageOptions gmo; ImqPutMessageOptions pmo; MQBYTE24 MsgId; MQLONG buflen; /* buffer length */ char buffer[MAX_BUFF_SIZE]; /* used for request */ char buffer2[MAX_BUFF_SIZE]; /* used for reply */ MQLONG qCC; cout << "Sample mqreq 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 and open the server queue. */ /*************************************************************/ qServer.setConnectionReference( qmgr ); qServer.setName( argv[ 1 ] ); qServer.setOpenOptions( MQOO_OUTPUT + MQOO_INQUIRE + MQOO_FAIL_IF_QUIESCING ); qServer.open(); /***************************************************************/ /* If there was an error opening the queue, print it out. */ /***************************************************************/ if (qServer.reasonCode()) { cout << "ImqQueue::open ended with reason code " << qServer.reasonCode( ) << endl; } if (qServer.completionCode()) { cout << "Unable to open queue for output" << endl; } /*************************************************************/ /* Open a dynamic queue for the replies that will come back. */ /*************************************************************/ qReply.setConnectionReference( qmgr ); qReply.setName( "SYSTEM.MQSC.REPLY.QUEUE" ); qReply.setDynamicQueueName("*"); qReply.setOpenOptions( MQOO_INPUT_EXCLUSIVE + MQOO_INQUIRE + MQOO_FAIL_IF_QUIESCING ); qReply.open(); /***************************************************************/ /* If there was an error opening the queue, print it out. */ /***************************************************************/ if (qReply.reasonCode()) { cout << "ImqQueue::open ended with reason code " << qReply.reasonCode( ) << endl; } if (qReply.completionCode()) { cout << "Unable to open queue for output" << endl; } cout << "Replies to: " << qReply.name() << endl; /****************************************************/ /* This program will be issuing requests and expect */ /* a reply back. */ /****************************************************/ msg.setMessageType(MQMT_REQUEST); msg.setReport(MQRO_EXCEPTION_WITH_DATA); msg.setReplyToQueueName(qReply.name()); /*****************************************************/ /* Keep looping as long as the user enters a message */ /*****************************************************/ qCC = qServer.completionCode( ); while ( qCC != MQCC_FAILED ) { cout << "Enter request, blank line to end" << endl; cin.getline(buffer, MAX_BUFF_SIZE); buflen = strlen(buffer); /****************************************************************/ /* If something was entered, put it on the server queue. */ /****************************************************************/ if (buflen > 0) { /*************************************************/ /* Set up the message buffer, format, and length */ /*************************************************/ msg.useEmptyBuffer(buffer, sizeof(buffer)); msg.setMessageId(); msg.setCorrelationId(); msg.setFormat(MQFMT_STRING); msg.setMessageLength(buflen); /*************************************************/ /* Put the message on the server queue and print */ /* out any errors that were encountered. */ /*************************************************/ if (!qServer.put(msg, pmo)) { cout << "ImqQueue::put ended with reason code " << qServer.reasonCode( ) << endl; } else { /************************************************/ /* Print out the message id that was used. The */ /* message id will be moved to the correlation */ /* id for the reply. */ /************************************************/ msg.messageId().copyOut(MsgId, MQ_MSG_ID_LENGTH, 0); cout << " Message ID from put: "; cout.setf(ios::hex); for (int i=0; i < MQ_MSG_ID_LENGTH; i++) { cout << (int)MsgId[i]; } cout << endl; cout.unsetf(ios::hex); /************************************************************/ /* Get the reply back from the server */ /************************************************************/ gmo.setOptions( MQGMO_WAIT | /* wait for new messages */ MQGMO_FAIL_IF_QUIESCING ); gmo.setWaitInterval( 5000 ); /* 5 second limit for waiting */ while ( qCC != MQCC_FAILED ) { msgReply.useEmptyBuffer( buffer2, sizeof( buffer2 ) ); /**************************************************************/ /* Set message id to null. The request message id will be */ /* moved to the correlation id for the get. */ /**************************************************************/ msgReply.setMessageId( ); msgReply.setCorrelationId(msg.messageId()); /*********************************************************/ /* Get the message. Print out the message or any errors */ /* that were received. */ /*********************************************************/ if ( qReply.get( msgReply, gmo ) ) { /******************************************************/ /* Display each message received */ /******************************************************/ if ( msgReply.formatIs( MQFMT_STRING ) ) { buffer2[ msgReply.dataLength( ) ] = 0 ; /* add terminator */ cout << " Reply message <" << msgReply.bufferPointer( ) << ">" << endl; } else { cout << " Non-text message" << endl; } } else { /* report reason, if any */ if ( qReply.reasonCode( ) == MQRC_NO_MSG_AVAILABLE ) { /* special report for normal end */ cout << " No more messages" << endl; break; } else { /* general report for other reasons */ cout << "ImqQueue::get ended with reason code" << (long)qReply.reasonCode( ) << endl; /* treat truncated message as a failure for this sample */ if ( qReply.reasonCode( ) == MQRC_TRUNCATED_MSG_FAILED ) { break ; } } qCC = qReply.completionCode( ); } } } qCC = qServer.completionCode( ); } else { break; /* empty line ... */ } } /****************************************************************/ /* Close the Server queue (if it was opened) */ /****************************************************************/ if ( !qServer.close()) { cout << "ImqQueue::server close ended with reason code " << qServer.reasonCode( ) << endl; } /****************************************************************/ /* Close the Reply queue (if it was opened) */ /****************************************************************/ if ( !qReply.close()) { cout << "ImqQueue::reply close ended with reason code " << qReply.reasonCode( ) << endl; } /****************************************************************/ /* Disconnect from MQM if not already connected */ /****************************************************************/ if (!qmgr.disconnect()) { cout << "ImqQueueManager::disconnect ended with reason code " << qmgr.reasonCode( ) << endl; } /******************************************************************/ /* End of mqreq */ /******************************************************************/ cout << "Sample mqreq C++ end" << endl; return(0); }