stomp-> hasFrame()返回false,队列中包含消息

When consuming data from a ActiveMQ queue im running into the following problem

With the following code:

$stomp = new Stomp($activeMQURI);
$stomp->subscribe($queue);
while ($stomp->hasFrame()) {

    $frame = $stomp->readFrame();

    if ($frame) {
        $stomp->ack($frame);
    }

}

It will only loop through about 1-10 messages before $stomp->hasFrame() returns false. The problem is there are 10k messages still in the queue!

When i put a delay in after the acknowledgment everything works as expected:

$stomp = new Stomp($activeMQURI);
$stomp->subscribe($queue);
while ($stomp->hasFrame()) {

    $frame = $stomp->readFrame();

    if ($frame) {
        $stomp->ack($frame);
        sleep(1);
    }

}

I was thinking that this was happening because the ActiveMQ server has not had a chance to process the ack before the consumer (my code) requests another frame. Can anyone explain the real reason why this is happening, and maybe a better fix then SLEEP?

You don't really specify what client you are using so here's a general answer. Most client's provide a blocking receive call either timed or infinite wait which will return when a message arrives, or indicate failure in the timed case. The speed at which the broker is going to dispatch messages to your client depends on a great many factors such as the number of consumers on the destination, the prefetch size set by each consumer, and the speed of the network etc, etc. Your code should not expect immediate turned and be able to deal with the case where there is a lull in message traffic. That's about as good an answer as I can give since I don't know any more about your setup.