我目前不得不使用MSMQ通过几台服务器发送大小为4MB的文件。这些文件最初是以块的形式发送的,如下所示:
using (MessageQueueTransaction oTransaction = new MessageQueueTransaction())
{
// Begin the transaction
oTransaction.Begin();
// Start reading the file
using (FileStream oFile = File.OpenRead(PhysicalPath))
{
// Bytes read
int iBytesRead;
// Buffer for the file itself
var bBuffer = new byte[iMaxChunkSize];
// Read the file, a block at a time
while ((iBytesRead = oFile.Read(bBuffer, 0, bBuffer.Length)) > 0)
{
// Get the right length
byte[] bBody = new byte[iBytesRead];
Array.Copy(bBuffer, bBody, iBytesRead);
// New message
System.Messaging.Message oMessage = new System.Messaging.Message();
// Set the label
oMessage.Label = "TEST";
// Set the body
oMessage.BodyStream = new MemoryStream(bBody);
// Log
iByteCount = iByteCount + bBody.Length;
Log("Sending data (" + iByteCount + " bytes sent)", EventLogEntryType.Information);
// Transactional?
oQueue.Send(oMessage, oTransaction);
}
}
// Commit
oTransaction.Commit();
}
这些消息从机器A发送到机器B,然后转发到机器C.但是,我注意到机器B上的PeekCompleted事件在所有消息发送之前被触发。
例如,刚刚进行的测试显示发送了8条消息,并在机器B上以1,1和6的组进行处理。
我认为这是由于交易部分确保消息以正确的顺序到达,但不保证它们都是在同一时间收集的。
我担心的是,当机器B将消息传递给机器C时,这些消息现在被视为3个单独的事务,并且我不确定事务本身是否以正确的顺序传递(例如, 1然后6然后1)。
我的问题是,是否可以通过事务使用PeekCompleted接收消息(意思是,首先收集所有8条消息),然后传递它们以便机器C将所有8条消息一起收集?即使在同时发送多个交易的系统中也是如此?
或者交易本身是否保证以正确的顺序到达?
答案 0 :(得分:0)
我认为在查看主题时我错过了这个:
https://msdn.microsoft.com/en-us/library/ms811055.aspx
这些消息将按照它们的顺序一起发送 是发送了,还是根本不发送。另外,连续交易 从同一台机器发起到同一个队列将到达 他们是相互承诺的。此外
因此,无论交易如何稀释,订单都不会受到影响。