本文介绍了如何停止使用来自选择性队列的消息 - RabbitMQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
QueueingConsumer consumer = new QueueingConsumer(channel);
System.out.println(consumer.getConsumerTag());
channel.basicConsume("queue1", consumer);
channel.basicConsume("queue3", consumer);
是否可以单独动态地停止消耗来自队列queue3"的消息?
Is it possible to stop consuming the messages from the queue "queue3" alone dynamically?
推荐答案
是的,你可以,使用 channel.basicCancel(consumerTag);
编辑例如:
Yes you can, using channel.basicCancel(consumerTag);
EDIT For example:
String tag3 = channel.basicConsume("queue3", consumer);
channel.basicCancel(tag3)
您可以在此处找到在 5 秒后取消订阅消费者的代码:
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
希望对大家有用.
这篇关于如何停止使用来自选择性队列的消息 - RabbitMQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!