Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop consuming message from selective queue - RabbitMQ

QueueingConsumer consumer = new QueueingConsumer(channel);
System.out.println(consumer.getConsumerTag());
channel.basicConsume("queue1", consumer);
channel.basicConsume("queue3", consumer);

Is it possible to stop consuming the messages from the queue "queue3" alone dynamically?

like image 511
user1182253 Avatar asked Apr 28 '14 06:04

user1182253


People also ask

How do I stop drinking RabbitMQ?

To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user... @googlegroups.com.

How do I stop duplicate messages in RabbitMQ?

You can enable de-duplication on a queue via setting its x-message-deduplication argument to true . Then, your publishers will need to provide the x-deduplication-header message header with a value meaningful for de-duplication. The value could be a unique message ID or the MD5 / SHA1 hash of the body for example.

How do I stop eating Pika?

You will have to cancel the consumer in your on_request method. You can also use this method to consume messages which allows an inactivity_timeout to be set, where you could then cancel your consumer.

Can two consumers consume the same message RabbitMQ?

RabbitMQ has a plugin for consistent hash exchange. Using that exchange, and one consumer per queue, we can achieve message order with multiple consumers. The hash exchange distributes routing keys among queues, instead of messages among queues. This means all messages with the same routing key will go the same queue.


1 Answers

Yes you can, using channel.basicCancel(consumerTag); EDIT For example:

String tag3 = channel.basicConsume("queue3", consumer);
channel.basicCancel(tag3)

Here you can find a code that unsubscribe a consumer after 5 seconds:

String tag1 = channel.basicConsume(myQueue, autoAck, consumer);
String tag2 = channel.basicConsume(myQueue2, autoAck, consumer);
    executorService.execute(new Runnable() {
        @Override
        public void run() {
            while (true) {
                Delivery delivery;
                try {
                    delivery = consumer.nextDelivery();
                    String message = new String(delivery.getBody());
                    System.out.println("Received: " + message);
                } catch (Exception ex) {
                    Logger.getLogger(TestMng.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    });
    System.out.println("Consumers Ready");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException ex) {
        Logger.getLogger(TestMng.class.getName()).log(Level.SEVERE, null, ex);
    }

    channel.basicCancel(tag2); /// here you remove only the Myqueue2

I hope it can be useful.

like image 112
Gabriele Santomaggio Avatar answered Sep 23 '22 07:09

Gabriele Santomaggio