MSMQ:How通过确认消息找出原始消息?
MSMQ:How to find out the original message by the acknowledgment message?
我正在通过指定的确认队列发送消息,这样我就可以确定消息是否已到达目标队列。
代码如下:
string queuePath = @"FormatName:DIRECT=TCP:127.0.0.1\PRIVATE$\testqueue";
string ackPath = @".\private$\ack";
MessageQueue msmq = new MessageQueue(queuePath,QueueAccessMode.SendAndReceive);
tran.Begin();
Message msg = new Message();
msg.UseAuthentication = false;
msg.Recoverable = true;
msg.Body = "HelloWorld";
msg.AcknowledgeType = AcknowledgeTypes.FullReachQueue;
msg.AdministrationQueue = new MessageQueue(ackPath, QueueAccessMode.ReceiveAndAdmin);
msmq.Send(msg, tran);
tran.Commit();
一切顺利,发送消息后,我可以在队列"ack"中找到确认消息。
但是我无法弄清楚确认消息和原始消息之间的关系。
所以我的问题是:如何将确认消息映射到原始消息?
您需要使用 System.Messaging.Message.CorrelationId
属性.
Gets or sets the message identifier used by acknowledgment, report,
and response messages to reference the original message.
来源:https://msdn.microsoft.com/en-us/library/system.messaging.message.correlationid(v=vs.110).aspx
var msgToSend = new Message();
// ... set message props including admin queue
var targetQueue = new MessageQueue(...);
targetQueue.Send(msgToSend);
// Read acknowledgment
var adminQueue = new MessageQueue(ackPath);
var msgAck = adminQueue.ReceiveByCorrelationId(msgToSend.Id, new TimeSpan(0, 0, 2));
if (msgAck)
{
return msgAck.Acknowledgment;
}
也有 PeekByCorrelationId
方法,或者 ReceiveByCorrelationId
的重载形式,您应该查看更多信息。
我正在通过指定的确认队列发送消息,这样我就可以确定消息是否已到达目标队列。 代码如下:
string queuePath = @"FormatName:DIRECT=TCP:127.0.0.1\PRIVATE$\testqueue";
string ackPath = @".\private$\ack";
MessageQueue msmq = new MessageQueue(queuePath,QueueAccessMode.SendAndReceive);
tran.Begin();
Message msg = new Message();
msg.UseAuthentication = false;
msg.Recoverable = true;
msg.Body = "HelloWorld";
msg.AcknowledgeType = AcknowledgeTypes.FullReachQueue;
msg.AdministrationQueue = new MessageQueue(ackPath, QueueAccessMode.ReceiveAndAdmin);
msmq.Send(msg, tran);
tran.Commit();
一切顺利,发送消息后,我可以在队列"ack"中找到确认消息。 但是我无法弄清楚确认消息和原始消息之间的关系。
所以我的问题是:如何将确认消息映射到原始消息?
您需要使用 System.Messaging.Message.CorrelationId
属性.
Gets or sets the message identifier used by acknowledgment, report, and response messages to reference the original message.
来源:https://msdn.microsoft.com/en-us/library/system.messaging.message.correlationid(v=vs.110).aspx
var msgToSend = new Message();
// ... set message props including admin queue
var targetQueue = new MessageQueue(...);
targetQueue.Send(msgToSend);
// Read acknowledgment
var adminQueue = new MessageQueue(ackPath);
var msgAck = adminQueue.ReceiveByCorrelationId(msgToSend.Id, new TimeSpan(0, 0, 2));
if (msgAck)
{
return msgAck.Acknowledgment;
}
也有 PeekByCorrelationId
方法,或者 ReceiveByCorrelationId
的重载形式,您应该查看更多信息。