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

发布于 2024-10-27 09:30:00 字数 1112 浏览 1 评论 0原文

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

This is my code and i set the true on autoDelete both queue , exchange finally publish is not sending any message to consumer several minute at this time i would like stop the consumer side automatically maybe you are not understand my sentence perfectly.

how can i setting that ^^

and how do I get document Object(doc) in server side

 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()");
  }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2024-11-03 09:30:00

您可以使用具有超时参数的 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
}

希望有所帮助。

You can use the overloaded version of nextDelivery() which has a timeout parameter:

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
}

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文