简体   繁体   中英

How to prioritize rabbitmq messages in php?

I'm using RabbitMQ (3.8.3 Erlang 22.3.1) for Laravel (6.18.*) . For this I'm using, https://github.com/vyuldashev/laravel-queue-rabbitmq .

With normal queue and consumer, everything is working fine. To prioritize messages, I've defined multiple queues with 0-3 as suffix in the queue names. I'm routing the jobs to different queues by manually calculating the total jobs.

Using this approach, for different tasks, I needed to create more queues with priorities on the name. Creating queues with 0-3 in the queue names does not seem scalable.

Now I'm trying to set priority per message. For that I tried to use priority property in AMQPMessage as:

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

I tried multiple messages with different priorities but priority doesn't seem to work at all.

  • I set x-max-priority on queue
  • I set priority to 1 for a AMQPMessage and dispatched 100K message from one terminal
  • Same time I dispatched another set of message of 10 with priority 2

But the consumer doesn't seem to consume message with priority 2.

Any idea what mistake am I doing? Please let me know if there's anything to set priorities per message so that consumer picks them first.

Try to use set, like that:

$headers = new AMQPTable([
    'x-cache-ttl' => 10 * 60000,
]);

$msg = new AMQPMessage($msg);
$msg->set('application_headers', $headers);
$msg->set('priority', 2);

And don't forget to declare queue with 'x-max-priority'

$options = new AMQPTable([
    'x-max-priority' => 3,
]);

$connection = new AMQPStreamConnection(RABBIT_HOST, RABBIT_PORT, RABBIT_LOGIN, RABBIT_PASS);
$channel = $connection->channel();
$channel->queue_declare('queue_name', false, false, false, false, false, $options);

When x-max-priority is declared for queue, it throws an exception.

operation queue.declare caused a channel exception precondition_failed: inequivalent arg 'x-max-priority' for queue 'my-queue in vhost '/': received the value '3' of type 'signedint' but current is none

Please let me know if i missed anything

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM