Интеграция приложений на основе WebSphere MQ


Example.9.1


/* Листинг программы rewriter */ /**********************************************************************/ /* Program name: Rewriter */ /* Description: Rewriter C program pass messages to output queue */ /* Function: */ /* Rewriter is a sample C program to demonstrate the main MQI calls; */ /* each message is copied from the input queue to the output */ /* queue, and sends a report to the log file */ /**********************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <signal.h> #include <io.h> /* includes for MQI */ #include <cmqc.h> char queue1[48] = ""; char queue2[48] = ""; char logfilename[48] = ""; char logfilename2[48] = ""; char buf[48]; int queuenamelen; time_t tmr; FILE * fp; FILE *fptr;

void cntrl_c_handler(int sig);

/* Declare MQI structures needed */ MQOD odG = {MQOD_DEFAULT}; /* Object Descriptor for GET */ MQOD odP = {MQOD_DEFAULT}; /* Object Descriptor for PUT */ MQOD odI = {MQOD_DEFAULT}; /* Object Descriptor for InitQ */ MQOD odR = {MQOD_DEFAULT}; /* Object Descriptor for report */ MQMD md = {MQMD_DEFAULT}; /* Message Descriptor */ MQGMO gmo = {MQGMO_DEFAULT}; /* get message options */ MQPMO pmo = {MQPMO_DEFAULT}; /* put message options */ MQTMC2 *trig; /* trigger message structure */ MQCHAR48 QManager; /* queue manager name */ MQHCONN Hcon; /* connection handle */ MQHOBJ Hobj; /* object handle, server queue */ MQHOBJ Hinq; /* handle for MQINQ */ MQHOBJ Hout; /* handle for MQPUT */ MQLONG O_options; /* MQOPEN options */ MQLONG C_options; /* MQCLOSE options */ MQLONG CompCode; /* completion code */ MQLONG Reason; /* reason code */ MQLONG CReason; /* reason code (MQCONN) */ MQBYTE buffer[8001]; /* message buffer */ MQLONG buflen; /* buffer length */ MQLONG messlen; /* message length received */ MQLONG Select[1]; /* attribute selectors */ MQLONG SelectValue[1]; /* value attribute selectors */ MQLONG char_count;

int main(int argc, char **argv) { strcpy(QManager, ""); /* Работаем с менеджером очередей по умолчанию */ if ( (fptr=fopen ("rewriter.ini","r" )) == NULL ) {printf("Cannot open rewriter.ini file" ); exit(1); } else{ /* Открываем ini-файл и присваиваем значения переменным */ fgets(queue1, 48, fptr); queuenamelen = strlen(queue1) - 1; queue1[queuenamelen] = ' '; fgets(queue2, 48, fptr); queuenamelen = strlen(queue2) - 1; queue2[queuenamelen] = ' '; fgets(logfilename, 48, fptr); queuenamelen = strlen(logfilename) - 1; logfilename[queuenamelen] = ' '; tmr = time(NULL); strcpy ( buf, ctime(&tmr)); buf[strlen(buf)-1]=0; // переход на новую строку strncat (logfilename, buf,10);

strcpy(odG.ObjectName, queue1); strcpy(odP.ObjectName, queue2); fclose (fptr); }

MQCONN(QManager, &Hcon, &CompCode, &CReason); if (CompCode == MQCC_FAILED) { printf("MQCONN ended with reason code %ld\n", CReason); exit(CReason); } O_options = MQOO_INPUT_SHARED + MQOO_FAIL_IF_QUIESCING; MQOPEN(Hcon, &odG, O_options, &Hobj, &CompCode, &Reason); /* открываем очередь для чтения - &odG */ if (Reason != MQRC_NONE) { printf("MQOPEN (input) ended with reason code %ld\n", Reason); } if (CompCode == MQCC_FAILED) { exit(Reason); }

O_options = MQOO_OUTPUT + MQOO_FAIL_IF_QUIESCING; MQOPEN(Hcon, &odP, O_options, &Hout, &CompCode, &Reason); /* открываем очередь для записи - &odP */ if (Reason != MQRC_NONE) { printf("MQOPEN (output) ended with reason code %ld\n", Reason); } if (CompCode == MQCC_FAILED) { exit(Reason); }

fp = fopen (logfilename,"a"); if ( fp==NULL ){ printf("Cannot open log file %s\n", logfilename); } printf("Rewriter(C) sending messages from %s to %s and to log-file %s \n",odG.ObjectName, odP.ObjectName, logfilename);

/*****************************************************************************/ /* Читаем сообщения из QUEUE_INPUT и пишем в QUEUE_OUTPUT */ /* до тех пор пока не встретим сообщение об ошибке */ /*****************************************************************************/ buflen = sizeof(buffer) - 1; while (CompCode == MQCC_OK) { gmo.Options = MQGMO_ACCEPT_TRUNCATED_MSG + MQGMO_WAIT; gmo.WaitInterval = 3000; /* Ожидаем новые сообщения 3 секунды */ //gmo.WaitInterval = MQWI_UNLIMITED; memcpy(md.MsgId, MQMI_NONE, sizeof(md.MsgId)); memcpy(md.CorrelId, MQMI_NONE, sizeof(md.CorrelId));

MQGET(Hcon, Hobj, &md, &gmo, buflen, buffer, &messlen, &CompCode, &Reason); if ((CompCode == MQCC_OK) || (CompCode == MQCC_WARNING)) { buffer[messlen] = '\0'; /* заносим символ конец строки в буфер с прочитанным сообщением */ buflen = messlen; MQPUT(Hcon, Hout, &md, &pmo, buflen, buffer, &CompCode, &Reason); if ((CompCode == MQCC_OK) || (CompCode == MQCC_WARNING)) { tmr = time(NULL); strcpy ( buf, ctime(&tmr)); buf[strlen(buf)-1]=0; // переход к новой строке Reason = fprintf( fp, "%s: %s\n", buf, buffer ); } } /* конец обработки входного сообщения */ } /* конец цикла чтения/записи сообщений функциями MQGET, MQPUT */

fclose (fp); C_options = 0; /* нет никаких опций при закрытии */ MQCLOSE(Hcon, &Hobj, C_options, &CompCode, &Reason); /* закрываем очередь для чтения */ if (Reason != MQRC_NONE) {printf("MQCLOSE (input) ended with reason code %ld\n", Reason); }

MQCLOSE(Hcon, &Hout, C_options, &CompCode, &Reason); /* закрываем очередь для записи */ if (Reason != MQRC_NONE) {printf("MQCLOSE (output) ended with reason code %ld\n", Reason); }

if (CReason != MQRC_ALREADY_CONNECTED) { MQDISC(&Hcon, &CompCode, &Reason); if (Reason != MQRC_NONE){ printf("MQDISC ended with reason code %ld\n", Reason); } }

return(0); }

Листинг 9.1. Rewriter C program pass messages to output queue

Закрыть окно




Начало  Назад  Вперед



Книжный магазин