user2264941
user2264941

Reputation: 407

Cannot read all messages from RabbitMQ

I create RabbitMQ listener:

$connection = new AMQPConnection(
    $AMQP_config['server'],
    $AMQP_config['port'],
    $AMQP_config['user'],
    $AMQP_config['password'],
    $AMQP_config['virtual_host']
);

$channel = $connection->channel();

$channel->basic_qos(
    null,
    1,
    null
);

$channel->basic_consume(
    $AMQP_config['queue'],
    '',
    false,
    false,
    false,
    false,
    array($this, 'CallbackResponse')
);

while(count($channel->callbacks)) {
    $channel->wait();
}

$channel->close();
$connection->close();

public function CallbackResponse(AMQPMessage $msg)
{
    $response = json_decode($msg->body)->acopMessage;
    if ($response->reqMRef == $this->_request_reference) {
        $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
    }
}

I have 5 messages on my RabbitMQ server. But I receive only one callback, only one entering into CallbackResponse().

I want to check all messages from the queue, find the one I've sent, read it, so there will be 4 messages left.

What I doing wrong, why I receive only first message?

Using this: https://github.com/videlalvaro/php-amqplib

Upvotes: 4

Views: 1579

Answers (1)

Nicolas Labrot
Nicolas Labrot

Reputation: 4106

Your QoS is set to 1. So RabbitMQ will only send one message at a time.

As you only ack the message you are expecting for, the first message you received which does not match you condition remains unack. Thus, RabbitMQ will not send new message.

So messages that do not match must be unacked and requeued

Upvotes: 1

Related Questions