简体   繁体   中英

Passing ids in IBM MQ client

I read the messages from one queue to another queue. However my correlation ids are not preserved.

If the correlation id is "ABC12345" for a message in the import queue, when i put it into the export queue, the value of the correlation id is different.

How do i keep the same correlation id between the 2 queues and always have a unique message id?

Get:

mqQueue.Get(mqMsg);
string messageID = Convert.ToString(mqMsg.MessageId);
string correlationID = Convert.ToString(mqMsg.CorrelationId);

If for example if the correlation id is "000123456789", then after read, while putting it back , the value gets changed for the same message.

Put:

 mqMsg.CorrelationId = System.Text.Encoding.UTF8.GetBytes(correlationID);
 mqQueue.Put(mqMsg, mqPutMsgOpts);

I am using MQ PUT and GET options via MQ.NET classes.

The code snippet below preserves the correlation id when I put message to another queue. In my sample I do the following:

1) Put a message to importQ with unique correlation ID.
2) Get that message from importQ .
3) Put the received message to exportQ

    public static void preserveCorreLid()
    {
        Hashtable mqProps = new Hashtable();
        MQQueueManager qm = null;
        String strCorrelId = "00123456789";

        try
        {
            mqProps.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
            mqProps.Add(MQC.CHANNEL_PROPERTY, "NET.CLIENT.CHL");
            mqProps.Add(MQC.HOST_NAME_PROPERTY, "localhost");
            mqProps.Add(MQC.PORT_PROPERTY, 2099);

            qm = new MQQueueManager("QM", mqProps);

            MQQueue importQ = qm.AccessQueue("IMPORTQ", MQC.MQOO_INPUT_SHARED |MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING );

            MQMessage mqPutMsg = new MQMessage();
            mqPutMsg.WriteString("This is an import message");
            mqPutMsg.CorrelationId = System.Text.Encoding.UTF8.GetBytes(strCorrelId);
            MQPutMessageOptions mqpmo = new MQPutMessageOptions();
            importQ.Put(mqPutMsg,mqpmo);

            MQMessage respMsg = new MQMessage();
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            gmo.WaitInterval = 3000;
            gmo.Options = MQC.MQGMO_WAIT;

            try
            {
                importQ.Get(respMsg, gmo);
            }
            catch (MQException ex)
            {
                Console.Write(ex);

                Console.WriteLine("Queue Name : " + importQ.Name + ":");
            }
            importQ.Close();

            MQQueue exportQ = qm.AccessQueue("EXPORTQ", MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING);
            exportQ.Put(respMsg);
            exportQ.Close();
            qm.Disconnect();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

这行代码为我提供了相关ID

  correlationID = System.Text.Encoding.UTF8.GetString(mqMsg.CorrelationId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM