在使用 NServiceBus 传奇时,我遇到了“乐观并发冲突”异常。
我的故事涵盖了一个相对简单的流程:当任何消息到达时,它都会发出很少的外部请求,并在一段时间后收集回复。
您可以在下面找到我的传奇示例。
NServiceBus 为此类流产生“乐观并发冲突”异常是正常行为吗?
我应该考虑以某种方式重新设计传奇吗?
class MySaga: SqlSaga<SagaData>, ...
{
CorrelationPropertyName => nameof(SagaData.UserId);
public Task Handle(StartSagaMessage message, IMessageHandlerContext context)
{
// save a new item id
Data.Items.Add(message.ItemId);
// make an external request for the item title
context.Send<GetItemTitle>(message.ItemId);
// make an external request for the item description
context.Send<GetItemDescription>(message.ItemId);
// gather results after one hour
RequestTimeout<RequestTimeout>(TimeSpan.FromHours(1));
}
// this method sometimes raises "optimistic concurrency violation" exception
public Task Handle(GetItemTitleResponse message, IMessageHandlerContext context)
{
Data.ItemTitles[message.ItemId] = message.ItemTitle;
}
// this method sometimes raises "optimistic concurrency violation" exception
public Task Handle(GetItemDescriptionResponse message, IMessageHandlerContext context)
{
Data.ItemDescriptions[message.ItemId] = message.ItemDescription;
}
public Task Handle(RequestTimeout state, IMessageHandlerContext context)
{
context.Send<ProcessItems>(Data.Items, Data.ItemTitles, Data.ItemDescriptions);
MarkAsComplete();
}
}
我在用:
- NServiceBus 6.4.2
- NServiceBus.RabbitMQ
- NServiceBus.Persistence.Sql