社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Akashdeep Singh

Akashdeep Singh 最近创建的主题
Akashdeep Singh 最近回复了
6 年前
回复了 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