在RabbitMQ PHP中设置消息优先级

时间:2015-06-18 17:52:41

标签: php rabbitmq message-queue

我在RabbitMQ for Java,Spring等中找到了很多设置消息优先级的例子,但到目前为止我还没有找到如何在PHP中实现它。

实际上,$channel->basic_publish()函数似乎不支持提供其他参数(https://github.com/videlalvaro/php-amqplib/blob/master/PhpAmqpLib/Channel/AMQPChannel.php),即使您可以在RabbitMQ gui中执行此操作。

有没有人在PHP中使用RabbitMQ的消息优先级?

2 个答案:

答案 0 :(得分:5)

好吧,它一直盯着我的脸。您可以在实际的message对象中设置优先级,而不是在将其推入队列时设置优先级:

$msg = new AMQPMessage("Hello World!", array(
    'delivery_mode' => 2,
    'priority' => 1,
    'timestamp' => time(),
    'expiration' => strval(1000 * (strtotime('+1 day midnight') - time() - 1))
));

答案 1 :(得分:0)

以下是AMQP Interop的示例。请注意,在声明队列时,不仅应设置优先级标头,还应设置特殊参数。

安装AMQP Interop兼容传输,例如

composer require enqueue/amqp-bunny

接下来做:

<?php
use Enqueue\AmqpBunny\AmqpConnectionFactory;
use Interop\Amqp\AmqpQueue;

$context = (new AmqpConnectionFactory())->createContext(); // connects to localhost with defaults

$queue = $context->createQueue("transcode2");
$queue->addFlag(AmqpQueue::FLAG_PASSIVE);
$queue->setArgument('x-max-priority', 10);
$context->declareQueue($queue);

$message = $context->createMessage(json_encode($msg));
$message->setPriority(5);

$producer = $context->createProducer($queue, $message);

$producer->send($queue, $message);
相关问题