ActiveMQ + Stomp,读取一条消息,但其中四条消息出列

时间:2012-10-19 09:09:57

标签: php queue activemq stomp

当我通过Stomp从AMQ读取一条消息时,我会将3或4条消息出列,但不知道为什么。

填充AMQ的代码:

public function populate($queue, $c = 10) {

    for($i = 0; $i < $c; $i++) {
        $this->stomp->send($queue,' Random populated:'.rand(0, PHP_INT_MAX));
    }

}

ActiveMQ Queue after populating ActiveMQ messages after populating

阅读AMQ的代码:

public function read($queue = null) {

    if(is_null($queue)) {
        if(!$this->isSubscribed()) {
            return false;
        }
    } else {
        $this->subscribe($queue);
    }

    return $this->stomp->readFrame();

}

Stomp readFrame()代码:

public function readFrame ()
{
    if (!$this->hasFrameToRead()) {
        return false;
    }

    $rb = 1024;
    $data = '';
    $end = false;

    do {
        $read = fread($this->_socket, $rb);
        if ($read === false) {
            $this->_reconnect();
            return $this->readFrame();
        }
        $data .= $read;
        if (strpos($data, "\x00") !== false) {
            $end = true;
            $data = rtrim($data, "\n");
        }
        $len = strlen($data);
    } while ($len < 2 || $end == false);

    list ($header, $body) = explode("\n\n", $data, 2);
    $header = explode("\n", $header);
    $headers = array();
    $command = null;
    foreach ($header as $v) {
        if (isset($command)) {
            list ($name, $value) = explode(':', $v, 2);
            $headers[$name] = $value;
        } else {
            $command = $v;
        }
    }
    $frame = new StompFrame($command, $headers, trim($body));
    if (isset($frame->headers['transformation']) && $frame->headers['transformation'] == 'jms-map-json') {
        require_once 'Stomp/Message/Map.php';
        return new StompMessageMap($frame);
    } else {
        return $frame;
    }
    return $frame;
}

我100%确定代码正在执行一次,但结果是: ActiveMQ queue after reading one message ActiveMQ messages after reading one message

Var_dumped消息:

object(StompFrame)[4]
public 'command' => string 'MESSAGE' (length=7)
public 'headers' => 
array (size=5)
  'message-id' => string 'ID:**********_-49723-1350635513276-2:1:-1:1:1' (length=45)
  'destination' => string '/queue/test' (length=11)
  'timestamp' => string '1350635842968' (length=13)
  'expires' => string '0' (length=1)
  'priority' => string '4' (length=1)
public 'body' => string 'Random populated:1859256320' (length=27)

有谁知道这种行为的原因是什么?

通知:

  • 没有消息,所以即使一条消息也不应该出列:|
  • ACK处于客户端模式
  • 预取大小设置为1

1 个答案:

答案 0 :(得分:3)

我没有看到你连接代码,但我假设你正在使用Auto Ack模式进行连接。在具有自动确认模式的STOMP中,消息一旦打到电话就会被激活,因为我还假设您没有更改预取大小,代理会向您发送一批消息,以便您从中读取消息套接字和更多可以发送更多将被出列。如果你想要对消息消费进行更细粒度的控制,你应该使用另一个ack模式,比如客户端ack和ack每个消息到达时。您还可以为订阅设置预取窗口,以减少批量发送到客户端的消息数。

有关AMQ STOMP配置选项,请参阅此page。您可能还想再次查看STOMP规范。