我有一個傳奇,有3個州;最初,ReceivingRows,已完成 -如何確保MassTransit中的郵件接收訂單
public static State Initial { get; set; }
public static State ReceivingRows { get; set; }
public static State Completed { get; set; }
它從初始轉換到ReceivingRows當它到達一個BofMessage(其中BOF文件=期初)。在BofMessage之後,它接收大量的RowMessages,每個RowMessage描述一個平面文件中的一行。一旦發送了所有RowMessages,就會發送一個EofMessage,並且狀態更改爲Completed。觀察 -
static void DefineSagaBehavior()
{
Initially(When(ReceivedBof)
.Then((saga, message) => saga.BeginFile(message))
.TransitionTo(ReceivingRows));
During(ReceivingRows, When(ReceivedRow)
.Then((saga, message) => saga.AddRow(message)));
During(ReceivingRows, When(ReceivedRowError)
.Then((saga, message) => saga.RowError(message)));
During(ReceivingRows, When(ReceivedEof)
.Then((saga, message) => saga.EndFile(message))
.TransitionTo(Completed));
}
這工作,但有時幾個RowMessages被的BofMessage之前收到!這與我發給他們的訂單無關。這意味着這些消息將被接收並最終計爲錯誤,導致它們從我最終寫出來的數據庫或文件中丟失。
作爲一個臨時的解決辦法,我在這個方法做所有出版加少許睡眠定時器黑客 -
public static void Publish(
[NotNull] IServiceBus serviceBus,
[NotNull] string publisherName,
Guid correlationId,
[NotNull] Tuple<string, string> inputFileDescriptor,
[NotNull] string outputFileName)
{
// attempt to load offsets
var offsetsResult = OffsetParser.Parse(inputFileDescriptor.Item1);
if (offsetsResult.Result != ParseOffsetsResult.Success)
{
// publish an offsets invalid message
serviceBus.Publish<TErrorMessage>(CombGuid.Generate(), publisherName, inputFileDescriptor.Item2);
return;
}
// publish beginning of file
var fullInputFilePath = Path.GetFullPath(inputFileDescriptor.Item2);
serviceBus.Publish<TBofMessage>(correlationId, publisherName, fullInputFilePath);
// HACK: make sure bof message happens before row messages, or else some row messages won't be received
Thread.Sleep(5000);
// publish rows from feed
var feedResult = FeedParser.Parse(inputFileDescriptor.Item2, offsetsResult.Offsets);
foreach (var row in feedResult)
{
// publish row message, unaligned if applicable
if (row.Result != ParseRowResult.Success)
serviceBus.Publish<TRowErrorMessage>(correlationId, publisherName, row.Fields);
else
serviceBus.Publish<TRowMessage>(correlationId, publisherName, row.Fields);
}
// publish end of file
serviceBus.Publish<TEofMessage>(correlationId, publisherName, outputFileName);
}
這是5秒的睡眠定時器,是一個相當醜陋的黑客。任何人都可以告訴我爲什麼我沒有收到我發給他們的訂單中的信息?如果默認情況下它們是無序的,我可以確保這些消息以正確的順序發送嗎?
謝謝!
請注意,這是從http://groups.google.com/group/masstransit-discuss/browse_thread/thread/7bd9518a690db4bb交叉發佈的權宜之計。