PHP Stomp:读取队列中的所有消息 [英] PHP Stomp: Reading all messages in a queue

查看:333
本文介绍了PHP Stomp:读取队列中的所有消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用PHP在ActiveMQ队列中查找特定消息并将其删除。

I need to find a specific message within an ActiveMQ queue using PHP and remove it.

AFAIK唯一的方法是读取当前排队的所有消息,并确认我感兴趣的一条消息。( Stomp :: ack 的PHP手册中的示例大致相同,但

AFAIK the only way to do so is to read all messages that are currently queued and ACK the one message I'm interested in. (The example in the PHP manual for Stomp::ack does more or less the same thing, they don't read all messages, but only ACK the one that matches).

因此,我编写了这段代码(显然,这只是相关部分):

So, I wrote this code (this is only the relevant part, obviously):

class StompController {

    private $con;

    public function __construct($stompSettings) {
        try {
            $this->con = new Stomp($stompSettings['scheme']."://".$stompSettings['host'].":".$stompSettings['port']);
            $this->con->connect();
            $this->con->setReadTimeout(5);
        } catch(StompException $e) {
            die('Connection failed:' .$e->getMessage());
        }
    }

    public function __destruct() {
        $this->con->disconnect();
    }

    public function ackMessageAsRead($recipient,$message) {
        if($this->con->isConnected()) {
            //Subscribe to the recipient user's message queue.
            $this->con->subscribe("/queue/".$recipient);
            //Read all messages currently in the queue (but only ACK the one we're interested in).
            while($this->con->hasFrameToRead()) {
                $msg = $this->con->readFrame();
                if($msg != null && $msg != false) {
                    //This is the message we are currently reading, ACK it to AMQ and be done with it.
                    if($msg->body == $message) {
                        $this->con->ack($msg);
                    }
                } 
            }
        } else {
            return false;
        }
    }
}

根据我的逻辑,应该管用。
在运行代码时,尽管检查了更多帧,但仅读取了一个随机 消息。

According to my logic, this should work. While running the code only one random message is being read though, despite checking for more frames.

下一个帧似乎只有在确认了我们当前正在阅读的帧之后才准备好。 (当我手动确认所有消息时, while 循环按预期工作时,所有消息都会得到处理。

The next frame seems only to be prepared when the frame we're currently reading has been ACK'ed. (When I manually ACK all messages the while loop works as intended, all messages are processed.

有人知道吗?如何从队列中获取全部消息而又不对所有消息进行ACK?我可以对所有消息进行ACK,然后将我不感兴趣的消息放回队列中,但是这种查找效率很低的方法这样,单个消息的效率就会大大降低。

Does anyone know how to get the full set of messages from the queue, without ACK'ing all of them? I can ACK all of them and put the ones I wasn't interested in back into the queue afterwards, but this already inefficient way to find a single message gets a whole lot more inefficient that way.

推荐答案

我认为这是由于设置 activemq.prefetchSize 到1。ActiveMQ使用预取大小来确定在任何时间点上可以将多少条消息分发给使用者。一旦达到预取大小,就不会再有更多消息被分发给使用者。直到消费者开始发回确认为止。增大预取大小应该可以解决您的问题。

I think this is an issue due to setting activemq.prefetchSize to 1. ActiveMQ uses a prefetch size on how many messages can be dispatched to a consumer at any point in time. Once the prefetch size is reached, no more messages are dispatched to the consumer until the consumer starts sending back acknowledgements. Increasing the prefetch size should fix your issue to my best knowledge.

请阅读 http://activemq.ap ache.org/what-is-the-prefetch-limit-for.html 了解有关预取限制的更多详细信息。

Please read http://activemq.apache.org/what-is-the-prefetch-limit-for.html for more details on prefetch limit.

这篇关于PHP Stomp:读取队列中的所有消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆