是否可以为 _error 队列中的消息设置生存时间?或者甚至跳过这种将消息发送到错误队列的机制,因为错误已经被消耗了?
我使用消费者和故障消费者来处理错误。我不再需要错误队列中的消息。
有可能,您可以创建一个过滤器来丢弃错误,然后配置错误管道来使用它。
首先,创建过滤器:
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");
}
}
或者甚至跳过这种将消息发送到错误队列的机制,因为错误已经被消耗了
你可以丢弃它们:
cfg.ReceiveEndpoint("input-queue", ec =>
{
ec.DiscardFaultedMessages();
});