msabhi
msabhi

Reputation: 81

How to browse the queue in rabbitmq without dequeuing the messages

I am trying to fetch a message with a particular correlation id like explained in rabbitmq docs. However I see that the irrelevant messages gets dequeued. I do not want it to happen. How can I tell rabbitmq to not dequeue after I get message and get to know that this is not the one I was looking for. Please help me.

`

.
.
replyQueueName = channel.queueDeclare().getQueue();
consumer = new QueueingConsumer(channel);
channel.basicConsume(replyQueueName, false, consumer);
while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            System.out.println(delivery.getProperties().getCorrelationId());
            if (delivery.getProperties().getCorrelationId().equals(corrId)) {
                response = new String(delivery.getBody());
                break;
            }
        }

`

Upvotes: 5

Views: 3865

Answers (2)

Derick Bailey
Derick Bailey

Reputation: 72858

You can't do what you want, the way you want. The "selective consumer" is an anti-pattern in RabbitMQ.

Instead, you should design your RabbitMQ setup so that your messages are routed to a queue that only contains messages for the intended consumer.

I wrote more about this, here: http://derickbailey.com/2015/07/22/airport-baggage-claims-selective-consumers-and-rabbitmq-anti-patterns/

Upvotes: 3

JVXR
JVXR

Reputation: 1312

If you can afford to lose the order of messages you can use the re-queueing mechanism.

Try turning off auto ack.

If not, you have to redesign your application to inject headers or routing keys to route to a particular queue.

Upvotes: 0

Related Questions