【问题标题】:How to set time to live for messages in _error queue with MassTransit?如何使用 MassTransit 为 _error 队列中的消息设置生存时间?
【发布时间】:2020-03-17 13:03:43
【问题描述】:

是否可以为 _error 队列中的消息设置生存时间?或者甚至跳过这种将消息发送到错误队列的机制,因为已经消耗了一个故障?
我正在使用消费者和错误消费者来处理错误。我不再需要错误队列中的消息。

【问题讨论】:

    标签: c# masstransit


    【解决方案1】:

    有可能,你可以创建一个过滤器来丢弃错误,然后配置错误管道来使用它。

    首先,创建过滤器:

        class DiscardExceptionFilter :
            IFilter<ExceptionReceiveContext>
        {
            public async Task Send(ExceptionReceiveContext context, IPipe<ExceptionReceiveContext> next)
            {
                await context.NotifyFaulted(context.Exception).ConfigureAwait(false);
    
                // not calling next.Send(), to end the pipe here.
            }
    
            public void Probe(ProbeContext context)
            {
                context.CreateScope("no-move-error");
            }
        }
    

    创建后,在接收端点上配置错误管道以使用过滤器。

    configurator.ConfigureError(x => x.UseFilter(new DiscardExceptionFilter()));
    

    【讨论】:

      【解决方案2】:

      我在这里做错了吗?无论异常类型如何,_error 队列中都没有任何内容。但是,如果我完全删除过滤器,正如预期的那样,所有消息最终都会进入错误队列。

      即使我尝试x.UseContextFilter(),也会发生同样的情况。

      e.ConfigureError(x =>
                              {
                                  x.UseFilter(new ExceptionLoggerAndFilter());
                                  //x.UseContextFilter(ec =>
                                  //{
                                  //    return Task.FromResult(!(ec.Exception is SqlException));
                                  //});
                                  
                              });
      
      #-----------------------------------------
      
      public class ExceptionLoggerAndFilter : IFilter<ExceptionReceiveContext>
          {
              public async Task Send(ExceptionReceiveContext context, IPipe<ExceptionReceiveContext> next)
              {
                  if (!(context.Exception is SqlException))
                  {
                      await context.NotifyFaulted(context.Exception).ConfigureAwait(false);
                      return;
                  }
      
                  await next.Send(context);
              }
      
              public void Probe(ProbeContext context)
              {
                  context.CreateScope("no-move-error");
              }
          }
      

      【讨论】:

      • 这并不能真正回答问题。如果您有其他问题,可以点击 进行提问。要在此问题有新答案时收到通知,您可以follow this question。一旦你有足够的reputation,你也可以add a bounty 来引起对这个问题的更多关注。 - From Review
      • 对不起,这是我的第一篇文章,所以不明白如何去做,或者它是如何工作的。
      猜你喜欢
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      相关资源
      最近更新 更多