doumeng4400 2018-06-09 06:53
浏览 129

为什么ActiveMQ通过Stomp向我的PHP使用者传递重复的消息?

I am not sure whether this question is related to stomp-php or ActiveMQ Docker (running with defaults).

I have a simple Queue helper class written in PHP that handles both sending the message to the queue (Queue::push), as well as consumes it (Queue::fetch). See code below.

As you can see, fetch() should subscribe to the queue, read one message and unsubscribe. The message should be acknowledged automatically (\Stomp\StatefulStomp::subscribe(), 3rd. argument).

For some reason, about 5-7% of the messages are received by the customer twice or even three times. Why messages are delivered multiple times and how to avoid it?


Publisher (pushing 1000 messages):

$mq = new Queue('tcp://activemq:61613','test');
for ($msgCount = 0; $msgCount < 1000; $msgCount++) {
    $mq->push('Message #' . $msgCount);
}

Consumer (receiving ~1070 messages):

$mq = new Queue('tcp://activemq:61613','test');
$received = 0;
while (true) {
    $message = $mq->fetch();
    if (null === $message) { break; }
    $received++;
}

Queue class code:

use Stomp\Client;
use Stomp\Network\Connection;
use Stomp\SimpleStomp;
use Stomp\StatefulStomp;
use Stomp\Transport\Message;

class Queue
{
    /**
     * @var \Stomp\StatefulStomp
     */
    private $stomp;

    private $queue;

    public function __construct($uri, $queue) {
        $connection = new Connection('tcp://activemq:61613');
        $this->stomp = new StatefulStomp(new Client($connection));
        $connection->setReadTimeout(1);
        $this->queue = $queue;
    }

    public function push($body) {
        $message = new Message($body, ['activemq.maximumRedeliveries' => 0]);
        $this->stomp->send('/queue/' . $this->queue, $message);
    }
    public function fetch() {
        $subscriptionId = $this->stomp->subscribe('/queue/' . $this->queue, null, 'auto', ['activemq.prefetchSize' => 1]);
        $msg = $this->stomp->read();
        $this->stomp->unsubscribe($subscriptionId);
        return $msg;
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的