如何在RABBITMQ中设置消费者方面的时间

时间:2011-03-28 09:09:58

标签: java xml multithreading string rabbitmq

这是我的代码,我在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()");
  }
 }

1 个答案:

答案 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
}

希望有所帮助。