我试图收听错误队列来处理失败的消息,但我似乎无法让MassTransit在我希望它在配置中监听的消息上设置绑定。配置如下,并使用MassTransit的v3:
var hostAddress = new Uri("rabbitmq://localhost/");
var username = "guest";
var password = "guest";
_busControl = MassTransit.Bus.Factory.CreateUsingRabbitMq(configurator =>
{
var host = configurator.Host(hostAddress, h =>
{
h.Username(username);
h.Password(password);
});
configurator.ReceiveEndpoint(host, "myqueue_error",
endpointConfigurator =>
{
endpointConfigurator.Handler<SomeMessage>(context =>
{
return Console.Out.WriteLineAsync("Woop");
});
});
});
在上面的示例中,它将为发布SomeMessage
的任何内容设置绑定,并将它们引导到myqueue_error
,我只希望消息进入此队列,该队列已从服务转发失败了。无论如何都要消耗队列中的消息,但是告诉MassTransit不要为它们绑定绑定吗?
更新 - 潜在解决方案
似乎我不需要设置ReceiveEndpoint,但我可以重命名控制总线以接受我关心的消息,然后这将能够处理这些消息而不创建与消息的交换绑定。
以下是更改后的代码,不确定这是否是一种理想的方式,但有效
var hostAddress = new Uri("rabbitmq://localhost/");
var username = "guest";
var password = "guest";
_busControl = MassTransit.Bus.Factory.CreateUsingRabbitMq(configurator =>
{
configurator.Host(hostAddress, h =>
{
h.Username(username);
h.Password(password);
});
// We need to make the queue look like the error queue
configurator.BusQueueName = $"{_queue}_error";
configurator.Durable = true;
configurator.AutoDelete = false;
configurator.SetQueueArgument("x-expires", null);
});
var connectHandle = _busControl.ConnectHandler<SomeMessage>(context => Console.Out.WriteLineAsync("Woop"));
_busHandle = _busControl.Start();
_busHandle.Ready.Wait();
// Wait
// Clean up
connectHandle.Disconnect();
_busHandle.Stop
答案 0 :(得分:2)
从大量的挖掘中我发现了一个更好的解决方案,我完全错过了文档。
似乎我们可以通过订阅消费者来收听消息来听取故障这对于我一直在努力实现的目标是完美的,我们也可以保持错误队列的大头钉。 http://docs.masstransit-project.com/en/mt3/usage/exceptions.html#handling-exceptions
所以我解决的最后一点配置如下:
var hostAddress = new Uri("rabbitmq://localhost/");
var username = "guest";
var password = "guest";
_busControl = MassTransit.Bus.Factory.CreateUsingRabbitMq(configurator =>
{
var host = configurator.Host(hostAddress, h =>
{
h.Username(username);
h.Password(password);
});
configurator.ReceiveEndpoint(host, "error_listener",
endpointConfigurator =>
{
endpointConfigurator.Handler<Fault<SomeMessage>>(context =>
{
return Console.Out.WriteLineAsync("Woop");
});
});
});