>RabbitMQ支持队列(又名队列TTL)的到期时间。如 RabbitMQ 文档中所述,通过将 x-expires
参数设置为queue_declare
方法,可以很容易地为给定队列设置到期时间。为了发送消息,我声明队列,然后使用 basic_publish
方法推送消息。但是,我的发送代码"触及"队列到期 - 当发送消息时,到期时间重置为其初始值(在我的示例中为 30 秒)。
这是我如何创建队列的简单示例:
$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
// queueKey is given as first arg
$queueName = $argv[1];
// auto-delete the queue after x ms
$channel->queue_declare($queueName, false, true, false, false, false,
new AMQPTable(array("x-expires" => 30000 ))
);
这就是我发送消息的方式:
$channel->queue_declare($queueName, true, true, false, false, false,
new AMQPTable(array("x-expires" => 30000 ))); // auto-delete the queue after x ms
// mark messages as persistent
$msg = new AMQPMessage($data, array(
'delivery_mode' => 2
));
// pushing the message to the queue
$channel->basic_publish($msg, '', $queueName);
发送消息时有什么方法不触及到期时间吗?我希望只有在某人真正消费消息(即有消费者)时才触摸到期时间,而不是在发送时。我的意思是,当"某人"发送消息时,我希望我的 30 秒计时器不会被重置。
谢谢!
您如何运行发布消息的脚本?
未使用表示队列 [...] 尚未重新声明
因此,如果每次运行生产者时都不断在代码中调用queue_declare
,计时器将被重置。
您可以在单独的脚本中声明队列,然后在生产者上不发出queue_declare
。