/***************************************************************************/ /* */ /* (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. */ /* */ /* Each copy of any portion of this sample program or any derivative */ /* work, must include a the above copyright notice and disclaimer of */ /* warranty. */ /* */ /***************************************************************************/ /* */ /* Program name: putgroup */ /* */ /* Description: Sample C program that puts messages in a group */ /* to a message queue using MQPUT */ /* */ /***************************************************************************/ /* */ /* Function: */ /* */ /* This program is a modified version of the AMQSPUT0.C program. */ /* It has been changed to put messages as part of a group. This */ /* is done by looking for the enter key being used at the end of */ /* each group. In addition, a second enter key is needed to end */ /* the program. */ /* */ /* For example, the following would be sample input: */ /* */ /* text entered comment */ /* ----------------------------- ------------------------------- */ /* 1st message group 1st message of group 1 */ /* group 1.2 2nd message of group 1 */ /* group 1.3 3rd message of group 1 */ /* indicate group 1 is finished */ /* 2nd message group 1st message of group 2 */ /* group 2.2 2nd message of group 2 */ /* group 2.3 3rd message of group 2 */ /* indicate group 2 is finished */ /* 3rd message group 1st message of group 3 */ /* group 3.2 2nd message of group 3 */ /* indicate group 3 is finished */ /* indicate we're done */ /* */ /* The program will generate an extra message with the enter key that */ /* ends the group that will be used to mark the end of the group. */ /* */ /***************************************************************************/ /* */ /* For a detailed description of what this program does, please refer */ /* to the original source for AMQSPUT0.C. */ /* */ /***************************************************************************/ /* */ /* putgroup has 2 parameters - */ /* - the name of the message queue (required) */ /* - the queue manager name (optional) */ /* */ /***************************************************************************/ #include #include #include #include int main(int argc, char **argv) { /* Declare file and character for sample input */ FILE *fp; /* Declare MQI structures needed */ MQOD od = {MQOD_DEFAULT}; /* Object Descriptor */ MQMD md = {MQMD_DEFAULT}; /* Message Descriptor */ MQPMO pmo = {MQPMO_DEFAULT}; /* put message options */ /** note, sample uses defaults where it can **/ MQHCONN Hcon; /* connection handle */ MQHOBJ Hobj; /* object handle */ MQLONG O_options; /* MQOPEN options */ MQLONG C_options; /* MQCLOSE options */ MQLONG CompCode; /* completion code */ MQLONG CC2; /* completion code for groups */ MQLONG OpenCode; /* MQOPEN completion code */ MQLONG Reason; /* reason code */ MQLONG CReason; /* reason code for MQCONN */ MQLONG buflen; /* buffer length */ char buffer[100]; /* message buffer */ char QMName[50]; /* queue manager name */ printf("Sample PUTGROUP start\n"); if (argc < 2) { printf("Required parameter missing - queue name\n"); exit(99); } /******************************************************************/ /* Connect to queue manager */ /******************************************************************/ QMName[0] = 0; /* default */ if (argc > 2) { strcpy(QMName, argv[2]); } MQCONN(QMName, &Hcon, &CompCode, &CReason); /* report reason and stop if it failed */ if (CompCode == MQCC_FAILED) { printf("MQCONN ended with reason code %ld\n", CReason); exit( (int)CReason ); } /******************************************************************/ /* Use parameter as the name of the target queue */ /******************************************************************/ strncpy(od.ObjectName, argv[1], (size_t)MQ_Q_NAME_LENGTH); printf("target queue is %s\n", od.ObjectName); /******************************************************************/ /* Open the target message queue for output */ /******************************************************************/ O_options = MQOO_OUTPUT /* open queue for output */ + MQOO_FAIL_IF_QUIESCING; /* but not if MQM stopping */ MQOPEN(Hcon, &od, O_options, &Hobj, &OpenCode, &Reason); /* report reason, if any; stop if failed */ if (Reason != MQRC_NONE) { printf("MQOPEN ended with reason code %ld\n", Reason); } if (OpenCode == MQCC_FAILED) { printf("unable to open queue for output\n"); } /******************************************************************/ /* */ /* Read lines from the file and put them to the message queue */ /* Loop until null line or end of file, or there is a failure */ /* */ /* Note: If you are using file redirection as input, the file */ /* MUST have two blank lines at the end of it. */ /* */ /******************************************************************/ CompCode = OpenCode; /* use MQOPEN result for initial test */ fp = stdin; memcpy(md.Format, /* character string format */ MQFMT_STRING, (size_t)MQ_FORMAT_LENGTH); while (CompCode != MQCC_FAILED) { if (fgets(buffer, sizeof(buffer), fp) != NULL) { buflen = strlen(buffer); /* length without null */ if (buffer[buflen-1] == '\n') /* last char is a new-line */ { buffer[buflen-1] = '\0'; /* replace new-line with null */ --buflen; /* reduce buffer length */ } } else { buflen = 0; /* treat EOF same as null line */ } /*************************************************************/ /* Put the message in the buffer out as the first message in */ /* the group. Ask the user for more messages to put out as */ /* part of the group. */ /*************************************************************/ if (buflen > 0) { /********************************************/ /* reset MsgId and CorrelId to get new ones */ /********************************************/ memcpy(md.MsgId, MQMI_NONE, sizeof(md.MsgId) ); memcpy(md.CorrelId, MQCI_NONE, sizeof(md.CorrelId) ); /****************************************************/ /* Message groups require a Version 2 MD. Supply a */ /* blank GroupId and let MQSeries generate it. */ /****************************************************/ md.Version = MQMD_VERSION_2; md.MsgFlags = MQMF_MSG_IN_GROUP; strcpy(md.GroupId, MQGI_NONE); /*********************************************************/ /* Message groups require a Version 2 PMO. In addition, */ /* put the entire group in syncpoint. */ /*********************************************************/ pmo.Version = MQPMO_VERSION_2; pmo.Options = MQPMO_LOGICAL_ORDER | MQPMO_SYNCPOINT; MQPUT(Hcon, Hobj, &md, &pmo, buflen, buffer, &CompCode, &Reason); if (Reason != MQRC_NONE) { printf("MQPUT ended with reason code %ld\n", Reason); } else { /************************************************************/ /* The first message got put out correctly. Display the */ /* GroupId (for reference) and then ask for more input that */ /* will be used to put out more messages in the group. */ /* When the user hits enter, generate one more message as */ /* the last message in the group. */ /************************************************************/ printf("-->GroupID <%-24.24s>\n", md.GroupId); printf("-->Hit enter when done with messages for group\n"); CC2 = MQCC_OK; while (CC2 != MQCC_FAILED) { if (fgets(buffer, sizeof(buffer), fp) != NULL) { buflen = strlen(buffer); if (buffer[buflen-1] == '\n') { buffer[buflen-1] = '\0'; --buflen; } } else { buflen = 0; /* treat EOF same as null line */ } if (buflen > 0) { md.MsgFlags = MQMF_MSG_IN_GROUP; MQPUT(Hcon, Hobj, &md, &pmo, buflen, buffer, &CC2, &Reason); if (Reason != MQRC_NONE) { printf("MQPUT in loop ended with reason code %ld\n", Reason); } } else { CC2 = MQCC_FAILED; } /* endif */ } /* endwhile */ /***********************************************************/ /* The next section of code will have the program generate */ /* a message that will be marked as the last message in */ /* the group. */ /***********************************************************/ md.MsgFlags = MQMF_LAST_MSG_IN_GROUP; strcpy(buffer, "Program generated last group message...."); buflen = strlen(buffer); MQPUT(Hcon, Hobj, &md, &pmo, buflen, buffer, &CompCode, &Reason); if (Reason != MQRC_NONE) { printf("MQPUT last ended with reason code %ld\n", Reason); } /********************************************************/ /* All messages in the group have been put. Commit the */ /* entire group. */ /********************************************************/ MQCMIT(Hcon, &CompCode, &Reason); if (Reason != MQRC_NONE) { printf("MQCMIT ended with reason code %ld\n", Reason); } printf("-->GroupID <%-24.24s> now finished.\n", md.GroupId); } } else { /* satisfy end condition when empty line is read */ CompCode = MQCC_FAILED; } } /******************************************************************/ /* Close the target queue (if it was opened) */ /******************************************************************/ if (OpenCode != MQCC_FAILED) { C_options = 0; /* no close options */ MQCLOSE(Hcon, &Hobj, C_options, &CompCode, &Reason); /* report reason, if any */ if (Reason != MQRC_NONE) { printf("MQCLOSE ended with reason code %ld\n", Reason); } } /******************************************************************/ /* Disconnect from MQM if not already connected */ /******************************************************************/ if (CReason != MQRC_ALREADY_CONNECTED) { MQDISC(&Hcon, &CompCode, &Reason); /* report reason, if any */ if (Reason != MQRC_NONE) { printf("MQDISC ended with reason code %ld\n", Reason); } } /******************************************************************/ /* END OF PUTGROUP */ /******************************************************************/ printf("Sample PUTGROUP end\n"); return(0); }