开发者

RabbitMQ Wait for a message with a timeout

开发者 https://www.devze.com 2023-01-17 04:03 出处:网络
I\'d like to send a message to a RabbitMQ server and then wait for a reply message (on a \"reply-to\" queue). Of course, I don\'t want to wait forever开发者_如何学JAVA in case the application processi

I'd like to send a message to a RabbitMQ server and then wait for a reply message (on a "reply-to" queue). Of course, I don't want to wait forever开发者_如何学JAVA in case the application processing these messages is down - there needs to be a timeout. It sounds like a very basic task, yet I can't find a way to do this. I've now run into this problem with Java API.


The RabbitMQ Java client library now supports a timeout argument to its QueueConsumer.nextDelivery() method.

For instance, the RPC tutorial uses the following code:

channel.basicPublish("", requestQueueName, props, message.getBytes());

while (true) {
    QueueingConsumer.Delivery delivery = consumer.nextDelivery();
    if (delivery.getProperties().getCorrelationId().equals(corrId)) {
        response = new String(delivery.getBody());
        break;
    }
}

Now, you can use consumer.nextDelivery(1000) to wait for maximum one second. If the timeout is reached, the method returns null.

channel.basicPublish("", requestQueueName, props, message.getBytes());

while (true) {
    // Use a timeout of 1000 milliseconds
    QueueingConsumer.Delivery delivery = consumer.nextDelivery(1000);

    // Test if delivery is null, meaning the timeout was reached.
    if (delivery != null &&
        delivery.getProperties().getCorrelationId().equals(corrId)) {
        response = new String(delivery.getBody());
        break;
    }
}


com.rabbitmq.client.QueueingConsumer has a nextDelivery(long timeout) method, which will do what you want. However, this has been deprecated. Writing your own timeout isn't so hard, although it may be better to have an ongoing thread and a list of in-time identifiers, rather than adding and removing consumers and associated timeout threads all the time.

Edit to add: Noticed the date on this after replying!


There is similar question. Although it's answers doesn't use java, maybe you can get some hints.

Wait for a single RabbitMQ message with a timeout


I approached this problem using C# by creating an object to keep track of the response to a particular message. It sets up a unique reply queue for a message, and subscribes to it. If the response is not received in a specified timeframe, a countdown timer cancels the subscription, which deletes the queue. Separately, I have methods that can be synchronous from my main thread (uses a semaphore) or asynchronous (uses a callback) to utilize this functionality.

Basically, the implementation looks like this:

//Synchronous case:
//Throws TimeoutException if timeout happens
var msg = messageClient.SendAndWait(theMessage);

//Asynchronous case
//myCallback receives an exception message if there is a timeout
messageClient.SendAndCallback(theMessage, myCallback);
0

精彩评论

暂无评论...
验证码 换一张
取 消