NServiceBus错误处理 [英] NServiceBus error handling

查看:121
本文介绍了NServiceBus错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白让NServiceBus处理错误,让它重试或使用第二级重试,这对应用程序中的一些未知错误是有效的。



例如让我们说一下transactionId == null,那么我不想抛出异常,让NService总线处理它,因为我知道这永远不会与任何额外的尝试。
对于知道的异常情况,这样做最好的是什么?



我使用的是传奇,它叫什么不同的端点(A,B,C,D)。如何处理这个端点中的上述错误情况,因为我不希望我的传奇处于挂起状态。

解决方案

如果你有一个异常(或异常类),你知道没有重试次数会有所帮助,那么你是正确的,最好是快速失败。



让我们说你有一个这样的处理程序..

  public void Handle(BadMath message)
{
var zero = 0;
var crash = 5 / zero;
var msg =我永远不会到达...;

this.SendReply(msg);
}

你想要陷阱&对于div / 0 ...失败快...

  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用NServiceBus;
使用NServiceBus.Features;
使用NServiceBus.SecondLevelRetries.Helpers;

命名空间My.Namespace.Messaging.Handlers
{
public class ChangeRetryPolicy:INeedInitialization
{
public void Init()
{
Configure.Features.Disable< SecondLevelRetries>();

Configure.Features.SecondLevelRetries(s => s.CustomRetryPolicy((tm)=>
{
// retry max 3 times
if(TransportMessageHelpers .GetNumberOfRetries(tm)> = 3)
{
//要发送一个小于零的值,请告知SecondLevelRetry
//卫星不再重试此消息
返回TimeSpan.MinValue;
}

if(tm.Headers [NServiceBus.ExceptionInfo.ExceptionType] == typeof(System.DivideByZeroException).FullName)
{
return TimeSpan.MinValue;
}
return TimeSpan.FromSeconds(5);
}));

Configure.Features
}
}

}

如果您尝试通过单元测试运行处理程序,测试当然会炸毁并抛出错误。
如果在
crash = 5 / zero的处理程序中放置一个断点,那么
然后重新运行您的项目,您应该会看到,当NServiceBus尝试重新传递消息时,第二个级别的重试不会发生,您的未发送将直接发送到错误队列。


I understand the let NServiceBus handles the error and let it retry or use the second level of retries this are valid for some unknown error within the applicaiton.

For example let say it the transactionId == null then i dont just want to throw the exception and let NService bus handles it as i know this never gonna pass with any extra attempts. For know exception scenarios what is the best this do?

I am using saga which call the what different endpoints (A,B,C,D). how do you handle the above error scenario in this endpoints as i dont want my saga to be in a hanging state.

解决方案

If you have an exception (or class of exceptions) that you know no number of retries will help, then you are correct, it is best to fail fast.

let's say you have a handler like this..

public void Handle(BadMath message)
{
   var zero = 0;
   var crash =  5 / zero;
   var msg = "I will never be reached...";

   this.SendReply(msg );
}

And you want to trap & fail fast for div/0...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Features;
using NServiceBus.SecondLevelRetries.Helpers;

namespace My.Namespace.Messaging.Handlers
{
    public class ChangeRetryPolicy : INeedInitialization
    {
        public void Init()
        {
            Configure.Features.Disable<SecondLevelRetries>();

            Configure.Features.SecondLevelRetries(s => s.CustomRetryPolicy((tm) => 
            {
                // retry max 3 times
                if (TransportMessageHelpers.GetNumberOfRetries(tm) >= 3)
                {
                    // To send back a value less than zero tells the SecondLevelRetry
                    // satellite not to retry this message anymore. 
                    return TimeSpan.MinValue;
                }

                if (tm.Headers["NServiceBus.ExceptionInfo.ExceptionType"] == typeof(System.DivideByZeroException).FullName)
                {
                    return TimeSpan.MinValue;
                }  
                return TimeSpan.FromSeconds(5);
            }));

            Configure.Features
        }
    }

}

If you try to run the handler via a unit test, the test will of course blow up and throw an error. If you put a break point in the handler on "crash = 5 / zero", then re-run your project you should see that when NServiceBus tries to redeliver the message, the second-level retries do not occur and your undelivered goes directly to the error queue.

这篇关于NServiceBus错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆