如何停止从选择性队列消耗消息 - RabbitMQ

时间:2014-04-28 06:20:51

标签: queue rabbitmq consumer

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

是否可以停止使用队列中的消息" queue3"单独动态?

1 个答案:

答案 0 :(得分:22)

是的,你可以使用channel.basicCancel(consumerTag);修改  例如:

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

在这里,您可以找到在5秒后取消订阅消费者的代码:

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

我希望它有用。