发现php-stomp扩展的小坑

发现php-stomp扩展的小坑

今天使用stomp对activemq的消息进行接收的时候发现一个php-stomp扩展的一个小坑,而官方文档并未明确给出,所以记录下来,分享给大家。

activemq两种形式

  1. 点对点模式(queue)

  2. 发布订阅模式(常用 topic)

    默认Stomp:send($queue)和Stomp::subscribe($queue)这里的$queue其实是指的是队列,即点对点模式;如果想对应topic就必须在前面加上一个 “/topic/”,例如java中topic名叫“local.test”,那么调用时候必须用
    Stomp:send("/topic/local.test")

activemq两种形式

topic模式需要先监听才能收到,必须是php先一直监听端口服务,然后java发送消息,顺序不能错,否则会收不到消息
可能原因:
摘自
	https://blog.csdn.net/weixin_30716141/article/details/97317975
	![在这里插入图片描述](https://img-blog.csdnimg.cn/20210201153951297.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l1bmR1YW4xNg==,size_16,color_FFFFFF,t_70)

所以正确调用方式是官网给出的demo:

        $clientId = 'test:dev:'.time();
        $stomp = new Stomp(self::$activemqConf['url'], self::$activemqConf['id'], self::$activemqConf['pswd'], array('client-id'=>
$clientId )); 
        $stomp->setReadTimeout(3);
        $isSubscribe = $stomp->subscribe(self::$activemqConf['queue']);
        //$isSubscribe 必须有
        while($isSubscribe) {
                if ($stomp->hasFrame()) {
                        $frame = $stomp->readFrame();
                        var_dump($frame);
                        if ($frame != NULL) {
                                print "Received: " . $frame->body . " - time now is " . date("Y-m-d
                                                H:i:s"). "\n";
                                $res = json_decode($frame->body, true);
                                $res = json_encode($res, JSON_UNESCAPED_UNICODE);
                                echo '<pre>';
                                print_r($res);
                                echo '</pre>';
                                //            $stomp->ack($frame);
                        }   
                        //       sleep(1);
                }   
                else {
                        //print "No frames to read\n";
                }   
        }   
        if($isSubscribe){
                $stomp->unsubscribe(self::$activemqConf['queue']);
                unset($stomp);
        }   

这样php才会一直监听服务,不会立即结束。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值