Deffering messages with nservicebus

二次信任 提交于 2019-12-31 04:19:04

问题


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.

I have a system that checks stock codes. It checks when the command is called and reschedules another check after 8pm.

I have CheckCurrentProductAvailabilityCommand that runs a function that checks stock codes. This is handled by CurrentProductAvailabilityRequestHandler.

If it fails I then run ScheduleCheckStockAvailabilityCommand with the stock code this is handled by ScheduleCheckStockCodeAvailabilityProcessor.

I then run a function _bus.Defer(_checkStockCodeAvailability.TimeOutTime, message.StockCode);

My actual code....

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;
                });
            }
        }

The code above works fine.

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.

Any help?


回答1:


In your code

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;
                });
            }
        }

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);

            }
        }


来源:https://stackoverflow.com/questions/19959138/deffering-messages-with-nservicebus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!