Reputation: 1037
Good day all, I have started (and I m really on newbie on that) some coding with rabbitMQ using php.
For now, I have just tested the examples on the Rabbitmq website. And they work. Problem is, if I send some messages with the producer before launching the consumer, the latter will not pick up previous messages. It only gets messages sent from the time it has started. So do I really have to always start the producer first? Isn't there a way to implement a queue that will wait until a consumer is available before flushing out the messages?
Or maybe the code requires an additional setting?
Thanks for your help
(The PHP code from rabbitMQ tutorial)
Producer.php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->exchange_declare('direct_logs', 'direct', false, false, false);
$severity = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'info';
$data = implode(' ', array_slice($argv, 2));
if(empty($data)) $data = "Hello World!";
$msg = new AMQPMessage($data);
$channel->basic_publish($msg, 'direct_logs', $severity);
echo " [x] Sent ",$severity,':',$data," \n";
$channel->close();
$connection->close();
?>
The consumer: The code for receive_logs_direct.php:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->exchange_declare('direct_logs', 'direct', false, false, false);
list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);
$severities = array_slice($argv, 1);
if(empty($severities )) {
file_put_contents('php://stderr', "Usage: $argv[0] [info] [warning] [error]\n");
exit(1);
}
foreach($severities as $severity) {
$channel->queue_bind($queue_name, 'direct_logs', $severity);
}
echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";
$callback = function($msg){
echo ' [x] ',$msg->delivery_info['routing_key'], ':', $msg->body, "\n";
};
$channel->basic_consume($queue_name, '', false, true, false, false, $callback);
while(count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();
?>
Upvotes: 1
Views: 479
Reputation: 10192
In short, no it must not. The queue however needs to exist and needs to be durable/auto-delete set to false.
I understand that this is for testing, but be careful when publishing messages that no one is consuming - the queue will end up being in flow state.
Upvotes: 2