无法读取RabbitMQ中的所有消息

时间:2022-06-27 09:49:15

I create RabbitMQ listener:

我创建RabbitMQ监听器:

$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().

我的RabbitMQ服务器上有5条消息。但我只收到一个回调,只有一个进入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.

我想检查队列中的所有消息,找到我发送的消息,然后阅读它,这样就会留下4条消息。

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

我做错了什么,为什么我只收到第一条消息?

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

使用此:https://github.com/videlalvaro/php-amqplib

1 个解决方案

#1


1  

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

您的QoS设置为1.因此RabbitMQ一次只发送一条消息。

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.

由于您只是收到了您期望的消息,因此您收到的第一条与您的条件不匹配的消息仍然无法解决。因此,RabbitMQ不会发送新消息。

So messages that do not match must be unacked and requeued

因此,必须将未匹配的消息取出并重新排队

#1


1  

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

您的QoS设置为1.因此RabbitMQ一次只发送一条消息。

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.

由于您只是收到了您期望的消息,因此您收到的第一条与您的条件不匹配的消息仍然无法解决。因此,RabbitMQ不会发送新消息。

So messages that do not match must be unacked and requeued

因此,必须将未匹配的消息取出并重新排队