2

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.

4

1 回答 1

0

如果您有一个异常(或一类异常),您知道重试次数无济于事,那么您是对的,最好快速失败。

假设您有这样的处理程序..

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

   this.SendReply(msg );
}

并且您想为 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
        }
    }

}

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

于 2013-08-30T13:45:13.170 回答