I need to catch just one actual message from one queue. Rabbit tries to catch all of them. Simplified code below:
private function getSingleTask(){
$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
$callback = function($msg) {
return $msg->body;
};
$channel->basic_qos(null, 1, null);
$channel->basic_consume('helloQueue', '', false, true, false, false, $callback);
$channel->wait(null, true, 5);
}
I throw few messages to the queue, but once i execute part of code below, it takes ALL messages from queue and $callbacks just the first.
The solution is easy...
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection;
$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$result = ($channel->basic_get('helloQueue', true, null)->body);
BTW second argument of "basic_get" method sets acknowledge for message, so with proper server settings it can tell you whether queue has messages or not, without getting a message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With