MQPUT消息长度

时间:2015-09-21 16:07:01

标签: visual-c++ ibm-mq

我有这个功能,我从队列中得到一条消息

int MQGet(MQHOBJ Hobj,char* buffer,int* count, long waitInterval, MQLONG codePage)
{
    MQLONG  msgLen = 0;
    MQLONG  msgSize = *count-1;
    MQGMO   gmo = {MQGMO_DEFAULT};
    MQMD    md = {MQMD_DEFAULT};    /* Message Descriptor            */

    gmo.Options =   MQGMO_WAIT 
                  | MQGMO_SYNCPOINT
                  | MQGMO_ACCEPT_TRUNCATED_MSG
                  | MQGMO_FAIL_IF_QUIESCING
                  | MQGMO_CONVERT ;

    gmo.MatchOptions = MQMO_NONE;

    //md.Format = ???
    //md.CodedCharSetId = 850;
    md.Encoding = MQENC_NATIVE;
    if( codePage != 0 )
            md.CodedCharSetId = codePage;

    gmo.WaitInterval = waitInterval;

       MQGET(Hcon,                /* connection handle               */
             Hobj,                /* object handle                   */
             &md,                 /* message descriptor              */
             &gmo,                /* default options (datagram)      */
             msgSize,             /* message length                  */
             buffer,              /* message buffer                  */
             &msgLen, 
             &CompCode,           /* completion code                 */
             &Reason);            /* reason code                     */

    *count = msgLen;
   /* if (CompCode == MQCC_FAILED) {
        return (int) Reason;
    }*/    
    return (int) Reason;
}

然后将消息放入队列

int MQPut(char* queueName,char* buffer,int count, bool persist, MQLONG codePage)
{
       MQOD     od = {MQOD_DEFAULT}; 
       strncpy(od.ObjectName, queueName, (size_t)MQ_Q_NAME_LENGTH);

       memcpy(md.Format,MQFMT_STRING, (size_t)MQ_FORMAT_LENGTH); /* character string format            */
       memcpy(md.MsgId,           /* reset MsgId to get a new one    */
              MQMI_NONE, sizeof(md.MsgId) );

       memcpy(md.CorrelId,        /* reset CorrelId to get a new one */
              MQCI_NONE, sizeof(md.CorrelId) );

//     memcpy(md.Encoding,        /* para forzar convert en destino */
//              MQENC_NATIVE, sizeof(md.Encoding) );
       md.Encoding = MQENC_NATIVE;

       if( persist )
           md.Persistence = MQPER_PERSISTENT; 


        if( codePage != 0 )
            md.CodedCharSetId = codePage;

//          memcpy(md.CodedCharSetId,        /* se agrega codificacion de caracteres a los mensajes */
//                codePage, sizeof(md.CodedCharSetId) );



 //      MQPUT(Hcon,                /* connection handle               */
 //            Hobj,                /* object handle                   */
 //            &md,                 /* message descriptor              */
 //            &pmo,                /* default options (datagram)      */
 //            count,               /* message length                  */
 //            buffer,              /* message buffer                  */
 //            &CompCode,           /* completion code                 */
 //            &Reason);            /* reason code                     */


             MQPUT1(Hcon,                /* connection handle               */
             &od, 
             &md,                 /* message descriptor              */
             &pmo,                /* default options (datagram)      */
             count,               /* message length                  */
             buffer,              /* message buffer                  */
             &CompCode,           /* completion code                 */
             &Reason);            /* reason code                     */


       if (CompCode == MQCC_FAILED) {
        return (int) Reason;
    }

    return 0;
}

我现在遇到的问题是我需要知道消息的长度是什么,

这是RunGet和RunPut服务

void CNTService::RunPut()
{
    _finddata_t fdata;
    char searchPath[MAX_PATH];
    char fname[MAX_PATH];
    char inputFileName[MAX_PATH];
    char outputFileName[MAX_PATH];
    char* t;
    char* f;
    intptr_t findHandle;
    MQHOBJ Hobj;
    DWORD status;
//  long error;

    // Set the service running flag

    m_bIsRunning = true;

    // Send a debug message that the service has started

    DebugMsg("Entering CNTService::Run()");

    // Create the search path for the find first routines

    strcpy(searchPath,pathIn);
    strcat(searchPath,"\\*.");
    strcat(searchPath,inSuffix);

    // Process the incomming files from the search path

    while (m_bIsRunning) {
        try {
            // Connect to MQSeries 

            Hobj = 0;
            if ((status = Connect(qMgrName,connName,channelName)) != 0) {
                throw CError(MSG_MQSERIES_OPEN,itoa(status,conv1,10));
            }

            // Open the queue for writing

/*          if ((Hobj = OpenQueue(queueName,error)) == 0) {
                throw CError(MSG_MQSERIES_QUEUE,queueName,itoa(error,conv1,10));
            }
*/
            do {
                // get a file to process

                if ((findHandle = _findfirst(searchPath,&fdata)) != -1) {
                    do {
                        char* buffer = new char[fdata.size];  // Allocate memory space for the file contents
                        HANDLE ifile;
                        DWORD bread;

                        // Read the file to memory
                        sprintf(fname,"%s\\%s",pathIn,fdata.name);

                        // Open the file

                        //ifile = open(fname,O_RDONLY|O_BINARY);

                        ifile = CreateFile(fname,
                                           FILE_READ_DATA,
                                           0,
                                           NULL,
                                           OPEN_EXISTING,
                                           0,
                                           0);

                        if (ifile != INVALID_HANDLE_VALUE) {
                            // Read in it's contents to the memory buffered
                            if (ReadFile(ifile,buffer,fdata.size,&bread,0) == 0) {
                                DWORD err = GetLastError();
                                //l(ifile);
                                throw CError(MSG_OPEN_ERROR,fname,itoa(err,conv1,10));
                            }

                            // Close the file handle

                            CloseHandle(ifile);

                            // Check that the read byte coun is equal to the file size

                            if (bread != fdata.size) {
                                CloseHandle(ifile);
                                LogWarning(CError(MSG_FILE_SIZE,itoa(bread,conv1,10),itoa(fdata.size,conv2,10)));
                                continue;
                            }
                        }
                        else {
                            LogWarning(CError(MSG_OPEN_ERROR,fname,itoa(GetLastError(),conv1,10)));
                            continue;
                        }


                        // Put the message into the MQSeries queue
                        //TODO: Recalcular tamaño del buffer
                        if ((status = MQPut(queueName,buffer,bread, persist, codePage)) != 0) {
                            throw CError(MSG_MQSERIES_PUT,itoa(status,conv1,10));
                        }

                        // Create the output file name

                        f = fdata.name;
                        t = inputFileName;
                        while (*f && *f !=  '.')
                            *t++ = *f++;
                        *t++ = 0;

                        sprintf(outputFileName,"%s\\%s%08d.%s",pathOut,inputFileName,GetTickCount(),inSuffix);;

                        // Rename the file to the queue directory

                        if (rename(fname,outputFileName) == -1)
                            throw CError(MSG_RENAME,fname,outputFileName);

                        // Free used heap 

                        delete[] buffer;

                    } while (_findnext(findHandle,&fdata) == 0);


                    // close the findfirst handle

                    _findclose(findHandle);
                }

                // Delay n milliseconds before processing the next request

                Sleep(delay);
            } while (m_bIsRunning);
        }

        // Trap thrown errors

        catch (CError err) {
           LogError(err);
        }

        // Trap unexpected errors

        catch (...)  {
           LogError(CError(MSG_SERVICE,"Error no esperado.",itoa(GetLastError(),conv1,10)));
        }

        // Close the queue handle

//      if (Hobj != 0) 
//          CloseQueue(Hobj); 

        // Disconnect from MQSeries

        Disconnect();

        // Delay for 5 seconds before reattempting to open MQSeries

        Sleep(5000);

    }


    // Debug message 
    DebugMsg("Leaving CNTService::Run()");
}

// Get messages from MqSeries queue and store them in the output directory

void CNTService::RunGet()
{
    char outputFileName[MAX_PATH];
    MQHOBJ Hobj;
    char tmpStr[512];    
    int count;
    long error;
    DWORD status;
    static int sequence = 0;

    // Initialize variables
    m_bIsRunning = true;
    // send a debug message theat the service is starting
    DebugMsg("Entering CNTService::Run()");

    // Set the localte to ascii
    setlocale(LC_ALL,"C");  /* for C with ancii 1 byte characters */

    // Process messages from the input queue
    while (m_bIsRunning) {
        try {
            // Connect to MQSeries
            Hobj = 0;
            if ((status = Connect(qMgrName, connName, channelName)) != 0) {
                throw CError(MSG_MQSERIES_OPEN,itoa(status,conv1,10));
            }
            // Open the input queue
            if ((Hobj = OpenGetQueue(queueName, error)) == -1) {
                throw CError(MSG_MQSERIES_QUEUE,queueName,itoa(error,conv1,10));
            }
            while (m_bIsRunning) {
                // Get the message from the input queue
                count = sizeof(buffer);

                error = MQGet(Hobj, buffer, &count, waitInterval, codePage);
                if (error == 0 || error == MQRC_TRUNCATED) {
                    int n;
                    int ofile;

                    // Log the file content to the system log 
                    sprintf(tmpStr,"count=%d,reason=%d",count,error);

                    if (logAll)
                        LogInfo(CError(MSG_SERVICE,outputFileName,tmpStr,""));

                    // Write the message contents to a file

                    sprintf(outputFileName,"%s\\MSG%08d%03d.%s", pathQueue, GetTickCount(), sequence, outSuffix);
                    sequence = ++sequence % 1000;

                    ofile = open(outputFileName, O_WRONLY|O_BINARY|O_TRUNC|O_CREAT,S_IREAD|S_IWRITE);
                    if (ofile == -1) {
                        throw CError(MSG_OPEN_ERROR,outputFileName,itoa(GetLastError(),conv1,10));
                    }

                    n = write(ofile, buffer, count);
                    // Close the file
                    close(ofile);

                    // Check if the read in bytes differ
                    if (n != count){ 
                        throw CError(MSG_FILE_WRITE,itoa(n,conv1,10),itoa(count,conv2,10));
                    }else
                        MQCMIT();
                }else {
                        if (m_bIsRunning) {
                            if (error != MQRC_NO_MSG_AVAILABLE) {
                                sprintf(tmpStr,"Error MQSeries reason=%d, error=",count,error);
                                throw CError(MSG_SERVICE,tmpStr,itoa(error,conv2,10));
                            }
                            Sleep(delay);
                        }
                }
            } 
        }
        // Catch thrown errors
        catch (CError err) {
           LogError(err);
           //revertimos mensaje a la cola
           MQBACK();
        }
        // Catch unexpected errors
        catch (...)  {
            LogError(CError(MSG_SERVICE,"Error no esperado."));
            MQBACK();
        }
        // Close the queue handle
        if (Hobj != 0) 
          CloseQueue(Hobj); 

        // Disconnect from mqseries
        Disconnect();

        // Delay until the next attemp
        Sleep(delay);
    }
    // nothing more to do
    DebugMsg("Leaving CNTService::Run()");
}

如果有任何身体可以提供帮助,我会很感激。 最好的问候

1 个答案:

答案 0 :(得分:2)

MQGET来电的名为msgLen的输出参数将告诉您邮件的实际长度。即使消息在get上被截断也是如此,因为您提供的缓冲区不够大。因此,允许您使用更大的缓冲区重试MQGET