我试图弄清楚即使没有消费者运行,是否可以将消息存储在 RabbitMQ 交换中。
我理解(可能是错误的)要实现交换需要“持久”以及队列,并且需要使用“持久”标志发送消息
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT
我的主要目标是将所有消息存储在交换器中,以便在无论出于何种原因没有消费者运行的情况下,当我启动一个交换器时,交换器中的所有消息都可以定向到绑定队列。我声明我的交换和队列如下:
//Sender.php
public function sendToQueue(ActionMessage $message)
{
$headers = [
'content-type' => 'application/json',
'timestamp' => $message->getCreatedAt()->getTimestamp(),
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT
];
$channel = $this->connection->getChannel();
$channel->exchange_declare($this->exchangeName, 'direct', false, true, false);
$qMessage = new AMQPMessage(json_encode($message->toArray()), $headers);
$channel->basic_publish($qMessage, $this->exchangeName, $message->getTopic());
return true;
}
//Receiver.php
public function consume($callbackFunction)
{
$channel = $this->messenger->getChannel();
$channel->exchange_declare($this->exchange, 'direct', false, true, false);
list($queueName, ,) = $channel->queue_declare('', false, true, true, false);
$channel->queue_bind($queueName, $this->exchangeName, $this->topicAction);
$channel->basic_consume($queueName, '', false, true, false, false, $callbackFunction);
while (count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$this->messenger->close();
}
我会很感激任何帮助(即使只是放弃这个想法并在两者之间插入一些存储)。谢谢。