【问题标题】:RabbitMQ not respecting job priorityRabbitMQ 不尊重工作优先级
【发布时间】:2016-04-28 05:24:54
【问题描述】:

我试图让RabbitMQ 在多队列、消息优先级方案中工作。但是,虽然我正确设置了作业的优先级,但工作进程不尊重消息中设置的优先级。

任务运行器:

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;

$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();

$channel->queue_declare('task_queue', false, true, false, false);

$i = 0;

while ($i <= 50000) {
    $p = rand(1, 255);
    $data = "Message #".$i." Priority: ".$p;
    $msg = new AMQPMessage($data,
                            array(
                                'delivery_mode' => 2, # make message persistent
                                'priority' => $p
                            )
                          );

    $channel->basic_publish($msg, '', 'task_queue');
    $i++;
}

$channel->close();
$connection->close();

工人:

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection;

$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();

$channel->queue_declare('task_queue', false, true, false, false);

echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

$callback = function($msg){
  echo " [x] Received ", $msg->body, " Priority: ", $msg->get('priority'),"\n";
  sleep(substr_count($msg->body, '.'));
  echo " [x] Done", "\n";
  $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};

$channel->basic_qos(null, 1, null);
$channel->basic_consume('task_queue', '', false, false, false, false, $callback);

while(count($channel->callbacks)) {
    $channel->wait();
}

$channel->close();
$connection->close();

这会触发 50k 个工作,然后工人“处理”它们。问题是,即使我迟到了工作人员,为了让队列填满不同优先级的工作,它仍然以先进先出的方式抓住它们。

我是否遗漏了一个标志或其他东西会使工人按最高优先级顺序拉动工作?

 [*] Waiting for messages. To exit press CTRL+C
 [x] Received Message #932 Priority: 107 
 [x] Done
 [x] Received Message #933 Priority: 5 
 [x] Done
 [x] Received Message #934 Priority: 183 
 [x] Done
 [x] Received Message #935 Priority: 231
 [x] Done
 [x] Received Message #936 Priority: 181 
 [x] Done

【问题讨论】:

    标签: php rabbitmq priority-queue


    【解决方案1】:

    要在 RabbitMQ 上使用优先级队列,您需要在声明队列时设置 x-max-priority 属性:

    https://www.rabbitmq.com/priority.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      相关资源
      最近更新 更多