私信  •  关注

Akashdeep Singh

Akashdeep Singh 最近创建的主题
Akashdeep Singh 最近回复了
5 年前
回复了 Akashdeep Singh 创建的主题 » 如何限制在rabbitmq中使用的消息数?

您的问题还不完全清楚,但您似乎希望限制服务如何使用消息,而不是队列包含多少消息。 如果这种理解是正确的,那么你需要的是 prefetch_count . 更多文档: https://www.rabbitmq.com/consumer-prefetch.html

var amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
    var q = 'task_queue';

    ch.assertQueue(q, {durable: true});
    ch.prefetch(1);                // THIS SHOULD SOLVE YOUR PROBLEM
    console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
    ch.consume(q, function(msg) {
      var secs = msg.content.toString().split('.').length - 1;

      console.log(" [x] Received %s", msg.content.toString());
      setTimeout(function() {
        console.log(" [x] Done");
        ch.ack(msg);
      }, secs * 1000);
    });
  });
});

代码源: https://www.rabbitmq.com/tutorials/tutorial-two-javascript.html