使用 NServiceBus 4.3 我想在某些情况出现时向错误队列发送消息。
场景是,当我收到一条消息时,我检查这条消息是否指的是我们数据库中的 1 个或多个项目。如果有多个引用,我会抛出一个AmbiguousItemException
并抓住它。我需要给负责给我正确信息的人发电子邮件。所有这些都已弄清楚,但我不希望再次尝试此消息。相反,我宁愿将它移到错误队列中,这样当我们取回我们需要的信息时,我们可以添加可空属性并将消息放回队列中进行处理。我试过使用_bus.ForwardCurrentMessageTo("error")
, _bus.Send("error", message)
, _bus.SendLocal(message)
. 最后一个基本上将消息置于无限循环中。代码有点像这样。
public class MoveToErrorQueue
{
private readonly IBus _bus;
public MoveToErrorQueue(IBus bus)
{
_bus = bus;
}
public virtual void Send(ResubmitMessage message)
{
message.Foo= -1;
_bus.Send("error", message);
}
}
以及调用它的代码
try
{
//removed for brevity
}
catch (AmbiguousItemException ex)
{
Log.Error(ex);
sendNotificationCommand.FailureMessage = ex.Message;
_moveToErrorQueue.Send(commandMesage);
}
SendNotification(sendScanningNotificationCommand);