rabbitmq

I want to use basicGet() for RPC call. Can it be made to block? Can it be interrupted?


On a project using RabbitMQ in a fairly old-fashioned RPC pattern I'm trying this on the server side:

ConnectionFactory factory = new ConnectionFactory();
factory.setUri(uri);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(queueName, false, false, false, null);
while (!shutdown) {
   GetResponse gr = channel.basicGet(queueName, true);
   ... build reply ...
   channel.basicPublish("", gr.getProps().getReplyTo(), replyProps, response);
}

Can the thread waiting on basicGet() be interrupted? If so, what happens (InterruptedException is not declared)? I realize this is not a great pattern, but I want some way to cleanly shutdown a service.

One comment indicates that basicGet() does not block at all and returns immediately if the queue is empty. If that is the case, how do I wait for a message on a queue and retrieve it, with a timeout?


Solution

  • basicGet doesn't block, it returns immediately (well, just after a network roundtrip) and returns null if there's no messages on the queue. So it's not necessary to interrupt the thread.