我正在尝试编写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.correaltionid匹配。
答案 0 :(得分:1)
您的代码段在阅读邮件时未显示设置相关信息。我有这个示例代码只获得给定correlId的消息匹配。
与之前一样,您的代码段MQC.MQOO_INQUIRE
仍为MQGMO
。 MQOO
代表Open Options
,而MQGMO
代表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));
}