How to stop message consumption from a 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 simply dynamically disconnect messages from the queue3 queue?

+7
queue rabbitmq
source share
1 answer

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

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

Here you can find the code that unsubscribes the user 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 

Hope this can be helpful.

+16
source share

All Articles