RabbitMQ 不尊重工作优先级
RabbitMQ not respecting job priority
我正在尝试让 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 个工作,然后工人 "handles" 他们。问题是,即使我启动工作人员的时间较晚,为了让队列填满不同优先级的作业,它仍然会以 FIFO 方式获取它们。
我是否遗漏了一个标志或其他可以让工作人员按最高优先级顺序提取作业的东西?
[*] 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
要在 RabbitMQ 上使用优先级队列,您需要在声明队列时设置 x-max-priority 属性:
我正在尝试让 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 个工作,然后工人 "handles" 他们。问题是,即使我启动工作人员的时间较晚,为了让队列填满不同优先级的作业,它仍然会以 FIFO 方式获取它们。
我是否遗漏了一个标志或其他可以让工作人员按最高优先级顺序提取作业的东西?
[*] 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
要在 RabbitMQ 上使用优先级队列,您需要在声明队列时设置 x-max-priority 属性: