PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 20, 2022

[FIXED] How to stop consuming message from selective queue - RabbitMQ

 September 20, 2022     consumer, queue, rabbitmq     No comments   

Issue

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?


Solution

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.



Answered By - Gabriele Santomaggio
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing