NServicebus 传奇竞争条件
我在使用 NServiceBus sagas 时遇到竞争条件。
问题是这样的 -
我有一个从 Saga 派生的类,实现 IAmStartedByMessages 并将 saga 数据保留在 sql 服务器中。这个传奇处理传入的消息,创建一条新消息,并将其发送到另一台服务器。该服务器处理消息并发回响应。然后,该回复消息将作为 saga 处理的下一阶段进行处理。我正在使用 saga ids 来配置映射。
当 saga 数据之前的回复消息已保存到数据库时,就会出现问题。 由于saga数据尚未保存,映射失败,消息丢失。
为了在下面的示例中进行说明,假设我让这些处理程序侦听不同的端点,我会在 SagaData 持久化之前收到 AnotherMessage。 -
public class MySaga : Saga<SagaData>,
IAmStartedByMessages<StartMesssage>
HandleMessage<AnotherMessage>
{
public override void ConfigureHowToFindSaga()
{
ConfigureMapping<AnotherMessage>(s => s.Id, x => x.SagaId);
}
void Handle(StartMessage message)
{
var sendMsg = new SendMessage(){SagaId=this.Data.Id}
bus.Send(sendMsg)
}
void Handle(AnotherMessage message)
{
}
}
public class NextStage : IHandleMessages<SendMessage>
{
void Handle(SendMessage message)
{
var anotherMsg = new AnotherMessage() {SagaId=message.SagaId};
bus.Send(anotherMsg);
}
}
(顺便说一句,我正在使用 NServiceBus 2.x(apache 许可版本)
问候,
Ilias
I am encountering a race condition when using NServiceBus sagas.
The problem is this-
I have a class that derives from Saga, implements IAmStartedByMessages and persists saga data in a sql server. This saga processes the incoming message, creates a new message, sends it on to another server. This server, processes the message and sends a response back. This reply message is then processed as the next stage of saga processing. I am using saga ids to configure the mapping.
Problem arises when the reply message immediately before the saga data has been saved to the database.
Since the saga data has not been saved, the mapping fails and the message is lost.
To illustrate in the following example, say I have these handlers listening on different endpoints, I get AnotherMessage before the SagaData has been persisted. -
public class MySaga : Saga<SagaData>,
IAmStartedByMessages<StartMesssage>
HandleMessage<AnotherMessage>
{
public override void ConfigureHowToFindSaga()
{
ConfigureMapping<AnotherMessage>(s => s.Id, x => x.SagaId);
}
void Handle(StartMessage message)
{
var sendMsg = new SendMessage(){SagaId=this.Data.Id}
bus.Send(sendMsg)
}
void Handle(AnotherMessage message)
{
}
}
public class NextStage : IHandleMessages<SendMessage>
{
void Handle(SendMessage message)
{
var anotherMsg = new AnotherMessage() {SagaId=message.SagaId};
bus.Send(anotherMsg);
}
}
(Btw, I am using NServiceBus 2.x (the apache licence edition)
Regards,
Ilias
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 saga 数据和bus.send 的持久化将是同一事务的一部分,因此我看不到任何竞争条件的可能性。您是否 100% 确定在回复到达之前数据不会被保留?
Since the persisting of the saga data and the bus.send will be part of the same transaction I don't see any posibilities for a race condition. Are you 100% sure that the data isn't persited before the reply arrives?