简体   繁体   中英

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;
            }
        }

`

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/

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.

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