这是我的代码,我在autoDelete上设置了真正的两个队列,交换终于发布不会向消费者发送任何消息几分钟此时我想自动停止消费者端,也许你完全不理解我的句子。
我该如何设置^^
如何在服务器端获取文档对象(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()");
}
}
答案 0 :(得分:5)
您可以使用具有超时参数的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
}
希望有所帮助。