有没有办法指定重试邮件的等待时间?

Yin*_*Yin 3 rebus

有没有办法指定为特定异常重试消息的等待时间?

例如,如果object处于SomethingInProgress状态,则抛出SomethignInProgressException,我希望消息在40m后重试.或者是否更适合引发SomethingInProgressEvent并使用bus.defer?

moo*_*000 5

这是Rebus没有二级重试概念的部分原因- 我根本没有看到任何方式可以以通用且足够灵活的方式创建此功能.

简要回答你的问题:不,没有(内置)方式来改变特定异常的重试之间的时间.事实上,有没有办法来配置重试之间的等待时间在所有 -失败的消息会以最快的速度可能重试,然后移动到错误队列,如果他们继续未能避免"堵塞管道".

在你的情况下,我建议你做这样的事情:

public void Handle(MyMessage message) {
    var headers = MessageContext.GetCurrent().Headers;
    var deliveryAttempt = headers.ContainsKey("attempt_no") 
        ? Convert.ToInt(headers["attempt_no"]) 
        : 0;

    try {
        DoWhateverWithThe(message);
    } catch(OneKindOfException e) {
        if (deliveryAttempt > 5) {
            bus.Advanced.Routing.ForwardCurrentMessage("error");
            return;
        }

        bus.AttachHeader(message, "attempt_no", deliveryAttempt + 1);
        bus.Defer(TimeSpan.FromSeconds(20), message);
    } catch(AnotherKindOfException e) {
        if (deliveryAttempt > 5) {
            bus.Advanced.Routing.ForwardCurrentMessage("error");
            return;
        }

        bus.AttachHeader(message, "attempt_no", deliveryAttempt + 1);
        bus.Defer(TimeSpan.FromMinutes(2), message);
    }
}
Run Code Online (Sandbox Code Playgroud)

我只是写下了我的头脑而没有100%确定它实际编译...但它的要点是我们跟踪我们在消息的自定义标题中进行了多少次传递尝试,bus.Defer响铃消息每次失败的传递尝试的适当时间跨度,当超过我们的最大传递尝试次数时立即将消息转发到错误队列.

我希望这是有道理的 :)

  • `IBus` 上所有发送消息的方法现在都接受一个可选参数 `optionalHeaders` - 例如`await bus.Send(message, new Dictionary<string, string>{{"custom-header", "wut"}})` (2认同)