开发者

In NServiceBus, how can I handle when a message comes in without a matching saga?

开发者 https://www.devze.com 2023-04-02 07:55 出处:网络
If I have a saga that con开发者_StackOverflowsists of two message types, say started by message1 and completed by message2, can I return a callback if a message2 comes in without a message1 already ex

If I have a saga that con开发者_StackOverflowsists of two message types, say started by message1 and completed by message2, can I return a callback if a message2 comes in without a message1 already existing? I know it will dump it in the error queue, but I want to be able to return a status to the sending client to say there is an error state due to the first message not being there.


So I figured it out, I just needed to implement IFindSagas for the message type:

public class MySagaFinder : IFindSagas<MySagaData>.Using<Message2>
{
    public ISagaPersister Persister { get; set; }
    public IBus Bus { get; set; }

    public MySagaFinder FindBy(Message2 message)
    {
        var data = Persister.Get<MySagaData>("MessageIdProperty", message.MessageIdProperty);
        if (data == null)
        {
            Bus.Return(0);
        }
        return data;
    }
}

I don't know if this is the right way to do it, but it works!


If you have a saga that can receive two messages, but messages can be received in any order, make sure the saga can be started by both messages. Then verify if both message have arrived by setting some state in the saga itself. If both messages have arrived, mark it as complete.

Default NServicebBus behavior is to ignore any message that has no corresponding saga. This is because you can set a timeout, for example. If nothing happens within 24 hours, the saga can send a Timeout message to itself. But if something did happen and you marked your saga as being completed, what should happen to the Timeout message? Therefor NServiceBus ignores it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号