Setting message priority in RabbitMQ PHP
I've found many examples of setting message priority in RabbitMQ for Java, Spring, etc., but so far I haven't found how to implement this in PHP.
In fact the function $channel->basic_publish()
does not support supplying additional parameters ( https://github.com/videlalvaro/php-amqplib/blob/master/PhpAmqpLib/Channel/AMQPChannel.php ) even though you can do it in the RabbitMQ gui.
Anyone got message priorities working with RabbitMQ in PHP?
source to share
Okay, it was staring me in the face the whole time. You are setting the priority on the actual object message
, not when you queue it:
$msg = new AMQPMessage("Hello World!", array(
'delivery_mode' => 2,
'priority' => 1,
'timestamp' => time(),
'expiration' => strval(1000 * (strtotime('+1 day midnight') - time() - 1))
));
source to share
Here's an example for AMQP Interop . Note that you must not only set a priority header but also a special argument when declaring a queue.
Install AMQP Interop compatible transport, for example
composer require enqueue/amqp-bunny
And do this:
<?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);
source to share