我试图让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();
这可以解雇5万个工作岗位然后工人处理&#34;他们。问题是,即使我开始工作较晚,允许队列填充不同优先级的作业,它仍然抓住它们的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
答案 0 :(得分:0)
要在RabbitMQ上使用优先级队列,您需要在声明队列时设置x-max-priority属性: