Passing ids in IBM MQ client

大兔子大兔子 提交于 2019-12-24 12:26:28

问题


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.


回答1:


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);
        }
    }



回答2:


This line of code gets me the correlation id

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


来源:https://stackoverflow.com/questions/15754550/passing-ids-in-ibm-mq-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!