简体   繁体   中英

Using ServiceStack and RabbitMQ to send messages from one queue to another

I have ServiceStack service registered as a message queue handler . While the service is handling a message and an error is encountered I would like to pull the remaining messages from the queue and send them to a different queue. Here is the code I am trying to use.

if (error)
{
    using (var mqClient = TryResolve<IMessageFactory>().CreateMessageQueueClient())
    {
        var callMessage = mqClient.Get<CallMessage>(QueueNames<CallMessage>.In);
        while (callMessage != null)
        {
        mqClient.Ack(callMessage);
        PublishMessage(new TextMessage { Text = callMessage.Text });

        // Get the next message. Null is returned when the queue is empty
        callMessage = mqClient.Get<CallMessage>(QueueNames<CallMessage>.In);
        }
    }
}

However, upon calling mqClient.Get the CallMessage queue seems to get deadlocked with an active Unacked message.

在此处输入图片说明

What is the proper technique for pulling messages from a queue and republishing them to a different queue?

You want to instead use IMessageQueueClient.GetAsync to retrieve a message from a queue, which will either return the next message or null if there are no more messages pending.

IMessageQueueClient.Get is a synchronous blocking Get that will block until it receives a message or the optional timeout has elapsed, if no timeout is given it will block forever until it receives a message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM