【问题标题】:Setting message priority in RabbitMQ PHP在 RabbitMQ PHP 中设置消息优先级
【发布时间】:2015-09-04 12:07:14
【问题描述】:

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

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

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

【问题讨论】:

    标签: php rabbitmq message-queue


    【解决方案1】:

    这是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);
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2012-05-31
        • 1970-01-01
        • 1970-01-01
        • 2021-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-29
        • 1970-01-01
        相关资源
        最近更新 更多