RabbitMQ:发送时未触及队列过期

RabbitMQ: Untouching the queue expiry upon sending

RabbitMQ 支持队列的到期时间(又名队列 TTL)。 As explained in RabbitMQ documentation,通过将 x-expires 参数设置为 queue_declare 方法,可以很容易地为给定队列设置到期时间。 为了发送消息,我声明了队列,然后使用 basic_publish 方法推送消息。 但是,我的发送代码 'touches' 队列过期 - 当发送消息时,过期时间重置为其初始值(在我的示例中为 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);

有没有办法在发送消息时不触及过期时间?我希望仅当有人真正使用消息(即有消费者)而不是发送时才触及到期时间。我的意思是,当 'someone' 发送消息时,我希望我的 30 秒计时器不会被重置。

谢谢!

您运行发布消息的脚本如何?

Unused means the queue […] has not been redeclared

因此,如果您每次 运行 您的制作人都在代码中调用 queue_declare,计时器将被重置。

您可以在单独的脚本中声明队列,然后在您的生产者上不要发出 queue_declare