How to Nak a ServiceStack RabbitMQ message within

2019-06-12 10:59发布

问题:

I'd like to be able to requeue a message from within my Service Endpoint that has been wired up through the RegisterHandler method of RabbitMQ Server. e.g.

mqServer.RegisterHandler<OutboundILeadPhone>(m =>
{
    var db = container.Resolve<IFrontEndRepository>();
    db.SaveMessage(m as Message);
    return ServiceController.ExecuteMessage(m);
}, noOfThreads: 1);

or here.

public object Post(OutboundILeadPhone request)
{
    throw new OutBoundAgentNotFoundException(); // added after mythz posted his first response
}

I don't see any examples how this is accomplished, so I'm starting to believe that it may not be possible with the ServiceStack abstraction. On the other hand, this looks promising.

Thank you, Stephen

Update

Throwing an exception in the Service does nak it, but then the message is sent to the OutboundILeadPhone.dlq which is normal ServiceStack behavior. Guess what I'm looking for is a way for the message to stay in the OutboundILeadPhone.inq queue.

回答1:

Throwing an exception in your Service will automatically Nak the message. This default exception handling behavior can also be overridden with RabbitMqServer's RegisterHandler API that takes an Exception callback, i.e:

void RegisterHandler<T>(
    Func<IMessage<T>, object> processMessageFn, 
    Action<IMessage<T>, Exception> processExceptionEx);

void RegisterHandler<T>(
    Func<IMessage<T>, object> processMessageFn, 
    Action<IMessage<T>, Exception> processExceptionEx, 
    int noOfThreads)