如何使用 MassTransit 设置 _error 队列中消息的生存时间?

问题描述 投票:0回答:3

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

c# masstransit
3个回答
0
投票

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

首先,创建过滤器:

    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()));

0
投票

我在这里做错了什么吗?无论异常类型如何,_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");
        }
    }

0
投票

或者甚至跳过这种将消息发送到错误队列的机制,因为错误已经被消耗了

你可以丢弃它们

cfg.ReceiveEndpoint("input-queue", ec =>
{
    ec.DiscardFaultedMessages();
});
© www.soinside.com 2019 - 2024. All rights reserved.