/***************************************************************************/ /* */ /* (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: mqsrv */ /* */ /* 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 mqreq */ /* 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 mqsrv.cpp imqb23vn.lib imqs23vn.lib */ /* */ /* Note: Use imqc23vn.lib instead of imqs23vn.lib for a client connect. */ /* */ /***************************************************************************/ /* */ /* mqsrv 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 qReply; ImqQueue qServer; ImqMessage msg; ImqMessage msgReply; ImqGetMessageOptions gmo; ImqPutMessageOptions pmo; MQBYTE24 MsgId; MQLONG buflen; /* buffer length */ char buffer[MAX_BUFF_SIZE]; /* used for message on puts/gets */ MQLONG qCC; cout << "Sample mqsrv 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. */ /*************************************************************/ qServer.setConnectionReference( qmgr ); qServer.setName( argv[ 1 ] ); qServer.setOpenOptions( MQOO_INPUT_AS_Q_DEF + 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 server queue for input" << endl; } qCC = qServer.completionCode( ); while ( qCC != MQCC_FAILED ) { msg.useEmptyBuffer( buffer, sizeof(buffer) ); /* buffer size available for GET */ gmo.setOptions( MQGMO_WAIT | /* wait for new messages */ MQGMO_FAIL_IF_QUIESCING ); gmo.setWaitInterval(15000 ); /* 15 second limit for waiting */ /**************************************************************/ /* Set message id and correlation id to null. The program */ /* takes whatever messages it can get. */ /**************************************************************/ msg.setMessageId( ); msg.setCorrelationId( ); if ( qServer.get( msg, gmo ) ) { /**************************************************************/ /* Display each message received */ /**************************************************************/ if ( msg.formatIs( MQFMT_STRING ) ) { buffer[ msg.dataLength( ) ] = 0 ; /* add terminator */ cout << "Message: <" << msg.bufferPointer( ) << ">" << endl; if (msg.messageType() != MQMT_REQUEST) { cout << " Not a request, no reply being sent" << endl; } else { cout << " RQmgr : " << msg.replyToQueueManagerName() << endl; cout << " RQueue: " << msg.replyToQueueName() << endl; cout << " Message ID from get: "; msg.messageId().copyOut(MsgId, MQ_MSG_ID_LENGTH, 0); cout.setf(ios::hex); for (int i=0; i < MQ_MSG_ID_LENGTH; i++) { cout << (int)MsgId[i]; } cout << endl; cout.unsetf(ios::hex); qReply.setConnectionReference( qmgr ); qReply.setName( msg.replyToQueueName() ); qReply.setOpenOptions( MQOO_OUTPUT + MQOO_INQUIRE + MQOO_FAIL_IF_QUIESCING ); qReply.open(); /***************************************************************/ /* If there was an error opening the queue, print it out. */ /***************************************************************/ if (qReply.reasonCode() || qReply.completionCode() ) { cout << "ImqQueue::Reply queue open ended with reason code " << qReply.reasonCode( ) << " and completion code " << qReply.completionCode() << endl; } else { msgReply.setMessageType(MQMT_REPLY); msgReply.setMessageId(); msgReply.setCorrelationId(msg.messageId() ); msgReply.useEmptyBuffer(buffer, sizeof(buffer)); msgReply.setFormat(MQFMT_STRING); strcpy(buffer, "mqsrv reply message"); buflen = strlen(buffer); msgReply.setMessageLength(buflen); if (!qReply.put(msgReply, pmo)) { cout << "ImqQueue::reply put ended with reason code " << qReply.reasonCode( ) << endl; } else { cout << " reply put for message" << endl; } if ( !qReply.close()) { cout << "ImqQueue::close ended with reason code " << qReply.reasonCode( ) << endl; } } } } else { cout << "Non-text message, no reply being sent" << endl; } } else { /* report reason, if any */ if ( qServer.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)qServer.reasonCode( ) << endl; /* treat truncated message as a failure for this sample */ if ( qServer.reasonCode( ) == MQRC_TRUNCATED_MSG_FAILED ) { break ; } } qCC = qServer.completionCode( ); } } /****************************************************************/ /* Close the target queue (if it was opened) */ /****************************************************************/ if ( !qServer.close()) { cout << "ImqQueue::close ended with reason code " << qServer.reasonCode( ) << endl; } /****************************************************************/ /* Disconnect from MQM if not already connected */ /****************************************************************/ if (!qmgr.disconnect()) { cout << "ImqQueueManager::disconnect ended with reason code " << qmgr.reasonCode( ) << endl; } /******************************************************************/ /* End of mqsrv */ /******************************************************************/ cout << "Sample mqsrv C++ end" << endl; return(0); }