Deffering messages with nservicebus

2020-04-18 07:07发布

问题:

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

            }
        }


标签: nservicebus