【发布时间】:2020-03-17 13:03:43
【问题描述】:
是否可以为 _error 队列中的消息设置生存时间?或者甚至跳过这种将消息发送到错误队列的机制,因为已经消耗了一个故障?
我正在使用消费者和错误消费者来处理错误。我不再需要错误队列中的消息。
【问题讨论】:
标签: c# masstransit
是否可以为 _error 队列中的消息设置生存时间?或者甚至跳过这种将消息发送到错误队列的机制,因为已经消耗了一个故障?
我正在使用消费者和错误消费者来处理错误。我不再需要错误队列中的消息。
【问题讨论】:
标签: c# masstransit
有可能,你可以创建一个过滤器来丢弃错误,然后配置错误管道来使用它。
首先,创建过滤器:
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()));
【讨论】:
我在这里做错了吗?无论异常类型如何,_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");
}
}
【讨论】: