我正在尝试从PHP使用Delayed Message Queue for RabbitMQ,但我的消息正在消失.
我用以下代码声明交换:
$this->channel->exchange_declare(
'delay',
'x-delayed-message',
false, /* passive, create if exchange doesn't exist */
true, /* durable, persist through server reboots */
false, /* autodelete */
false, /* internal */
false, /* nowait */
['x-delayed-type' => ['S', 'direct']]);
我用这段代码绑定队列:
$this->channel->queue_declare(
$queueName,
false, /* Passive */
true, /* Durable */
false, /* Exclusive */
false /* Auto Delete */
);
$this->channel->queue_bind($queueName, "delay", $queueName);
我正在使用以下代码发布消息:
$msg = new AMQPMessage(json_encode($msgData), [
'delivery_mode' => 2,
'x-delay' => 5000]);
$this->channel->basic_publish($msg, 'delay', $queueName);
但消息不会延迟;它仍然立即交付.我错过了什么?
解决方法:
消息创建应该是
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;
$msg = new AMQPMessage($data,
array(
'delivery_mode' => 2, # make message persistent
'application_headers' => new AMQPTable([
'x-delay' => 5000
])
)
);
标签:php,rabbitmq,php-amqplib
来源: https://codeday.me/bug/20190727/1557763.html