gpt4 book ai didi

java - 如何在 RABBITMQ 中为消费者端设置时间

转载 作者:行者123 更新时间:2023-12-01 16:39:23 26 4
gpt4 key购买 nike

这是我的代码,我在 autoDelete 两个队列上设置了 true ,交换最终发布在几分钟内没有向消费者发送任何消息,此时我想自动停止消费者端,也许你没有完全理解我的句子。

我该如何设置^^

以及如何在服务器端获取文档对象(doc)

 public void initConsumer() {
try {
ConnectionFactory factory = new ConnectionFactory();
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(this.queueName, this.maintain, false, this.queueAutoDelete, null);
channel.exchangeDeclare(this.exchangeName, this.exchangeType, this.maintain, this.exchangeAutoDelete, null);
channel.queueBind(this.queueName, this.exchangeName, this.routingKey);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(this.queueName, false, consumer);
while (true) {

QueueingConsumer.Delivery delivery = consumer.nextDelivery();

System.out.println(" [x] Received "
+ new String(delivery.getBody()));

channel
.basicAck(delivery.getEnvelope().getDeliveryTag(),
false);
}
} catch (Exception e) {
System.out.println("Exception error at initConsumer()");
}
}

最佳答案

您可以使用具有超时参数的 nextDelivery() 的重载版本:

QueueingConsumer.Delivery delivery = null;
long timeout = 2 * 60 * 1000; // 2 minutes in milliseconds
delivery = queuingConsumer.nextDelivery(timeout);
if (delivery == null) {
// shut down your consumer here - no events arrived
// before the timeout was reached
}
else {
// process the delivered message here
}

希望有帮助。

关于java - 如何在 RABBITMQ 中为消费者端设置时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5456808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com