使用 nservicebus 延迟消息 [英] Deffering messages with nservicebus
问题描述
我正在处理 nServiceBus,我想在另一个失败时重试并发送一条消息.我听说过 Bus.Defer() 但我对它的理解有限.
I am dealing with nServiceBus and I want to retry and a message at another time when one fails. I have heard of Bus.Defer() but my understanding of it is limited.
我有一个检查股票代码的系统.它检查命令何时被调用并在晚上 8 点之后重新安排另一次检查.
I have a system that checks stock codes. It checks when the command is called and reschedules another check after 8pm.
我有 CheckCurrentProductAvailabilityCommand 运行检查股票代码的函数.这由 CurrentProductAvailabilityRequestHandler 处理.
I have CheckCurrentProductAvailabilityCommand that runs a function that checks stock codes. This is handled by CurrentProductAvailabilityRequestHandler.
如果失败,我会使用由 ScheduleCheckStockCodeAvailabilityProcessor 处理的股票代码运行 ScheduleCheckStockAvailabilityCommand.
If it fails I then run ScheduleCheckStockAvailabilityCommand with the stock code this is handled by ScheduleCheckStockCodeAvailabilityProcessor.
然后我运行一个函数 _bus.Defer(_checkStockCodeAvailability.TimeOutTime, message.StockCode);
I then run a function _bus.Defer(_checkStockCodeAvailability.TimeOutTime, message.StockCode);
我的实际代码....
if (scheduleRecheck && result.ErrorMessage.Equals("Bad Request"))
{
Logger.Error("Stock query for stock code '{0}' returned a 'Product Not Found' status",
stockcode);
_bus.SendLocal<ScheduleCheckStockAvailabilityCommand>(cmd =>
{
cmd.StockCode = stockcode;
});
}
}
上面的代码工作正常.
public class ScheduleCheckStockCodeAvailabilityProcessor : IHandleMessages<ScheduleCheckStockAvailabilityCommand>
{
readonly ICheckStockCodeAvailability _checkStockCodeAvailability;
readonly IBus _bus;
public ScheduleCheckStockCodeAvailabilityProcessor(ICheckStockCodeAvailability checkStockCodeAvailability, IBus bus)
{
_checkStockCodeAvailability = checkStockCodeAvailability;
_bus = bus;
}
public void Handle(ScheduleCheckStockAvailabilityCommand message)
{
_bus.Defer(_checkStockCodeAvailability.TimeOutTime, message.StockCode);
}
}
但我无法从逻辑上想到这将如何运作.
But I can't think of logically how this would work.
有什么帮助吗?
推荐答案
在你的代码中
if (scheduleRecheck && result.ErrorMessage.Equals("Bad Request"))
{
Logger.Error("Stock query for stock code '{0}' returned a 'Product Not Found' status",
stockcode);
_bus.SendLocal<ScheduleCheckStockAvailabilityCommand>(cmd =>
{
cmd.StockCode = stockcode;
});
}
}
您正在发送消息 ScheduleCheckStockAvailabilityCommand.现在,在您的其他函数中,yoe 正在推迟此消息,即 ScheduleCheckStockAvailabilityCommand (我认为只有在 CheckCurrentProductAvailabilityCommand 出现错误时才会调用它).因此,根据我收集的信息,您想推迟 CheckCurrentProductAvailabilityCommand 而不是 ScheduleCheckStockAvailabilityCommand 所以我认为您的代码应该是:
you are sending a message ScheduleCheckStockAvailabilityCommand. Now in your other function yoe are deferring this message i.e. ScheduleCheckStockAvailabilityCommand (which i think is called only when there is some error with CheckCurrentProductAvailabilityCommand ). So according to what I gather you want to defer CheckCurrentProductAvailabilityCommand rather than ScheduleCheckStockAvailabilityCommand so I think your code should be:
if (scheduleRecheck && result.ErrorMessage.Equals("Bad Request"))
{
Logger.Error("Stock query for stock code '{0}' returned a 'Product Not Found' status",stockcode);
_bus.Defer(_checkStockCodeAvailability.TimeOutTime, CheckCurrentProductAvailabilityCommand);
}
}
这篇关于使用 nservicebus 延迟消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!