0

我正在尝试编写 2 种方法来测试一个队列中发送的消息是否在另一个队列中接收。

发送方法 - 发送消息,例如 - “消息 123” - 以导出具有唯一相关 ID 的队列。

获取方法

这个队列将有很多消息,但是我只想根据我的相关 ID 获取我从上面发送的消息。

基于相关ID检查消息的代码

      properties = new Hashtable();
       properties.Add(MQC.CONNECTION_NAME_PROPERTY, "connection name");
       properties.Add(MQC.TRANSPORT_PROPERTY, "transport type");
       properties.Add(MQC.CHANNEL_PROPERTY, "channel name"); 
       properties.Add(MQC.CONNECT_OPTIONS_PROPERTY, MQC.MQCNO_HANDLE_SHARE_BLOCK);



       mqGetMsgOpts = new MQGetMessageOptions();
       mqGetMsgOpts.Options = MQC.MQGMO_BROWSE_FIRST | MQC.MQGMO_WAIT | MQC.MQOO_INQUIRE;
       mqGetMsgOpts.MatchOptions = MQC.MQMO_MATCH_CORREL_ID;
       mqGetMsgOpts.WaitInterval = 3000; //3 secs wait time

我面临的问题是当我阅读消息时,我从导入队列中获取所有消息。

如何仅获取我发送的消息并验证导出队列中收到的消息是我的?

从理论上讲,像这样

导入队列中的 message.correlationid 与导出队列中的 message.correlationid 匹配。

4

1 回答 1

1

您的代码段在阅读消息时未显示设置 correlId。我有这个示例代码,它只获取与给定 correlId 匹配的消息。

像之前一样,您的代码段仍然具有MQC.MQOO_INQUIREfor MQGMOMQOO代表Open OptionsMQGMO代表Get message options

        try
        {
            importQ = qm.AccessQueue("Q2", MQC.MQOO_INPUT_SHARED | MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING);

            // Put one message. MQ generates correlid
            MQMessage mqPutMsg = new MQMessage();
            mqPutMsg.WriteString("This is the first message with no app specified correl id");
            importQ.Put(mqPutMsg);

            // Put another messages but with application specified correlation id
            mqPutMsg = new MQMessage();
            mqPutMsg.WriteString("This is the first message with application specified correl id");
            mqPutMsg.CorrelationId = System.Text.Encoding.UTF8.GetBytes(strCorrelId);
            MQPutMessageOptions mqpmo = new MQPutMessageOptions();
            importQ.Put(mqPutMsg,mqpmo);

            mqPutMsg = new MQMessage();

            // Put another message with MQ generating correlation id
            mqPutMsg.WriteString("This is the second message with no app specified correl id");
            importQ.Put(mqPutMsg);

            // Get only the message that matches the correl id
            MQMessage respMsg = new MQMessage();
            respMsg.CorrelationId = System.Text.Encoding.UTF8.GetBytes(strCorrelId);
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            gmo.WaitInterval = 3000;
            gmo.Options = MQC.MQGMO_WAIT;
            gmo.MatchOptions = MQC.MQMO_MATCH_CORREL_ID;

            importQ.Get(respMsg, gmo);
            Console.WriteLine(respMsg.ReadString(respMsg.MessageLength));
        }
于 2013-04-26T03:50:43.740 回答