手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

PHP和ActiveMQ集成

[日期:2013-08-01] 来源:Linux社区  作者:iefreer [字体:大 中 小]

假设你喜欢用PHP构建WEB应用,然后还有一些外围的应用,包括移动终端APP,mobile web等,由于mobile app等是非PHP语言所编写如ObjectiveC、Java等,然后你想在这些客户端应用程序之间共享某些基础服务,希望这些基础服务以一种松耦合,高扩展性,高性能的方式来提供,那么一个比较好的解决方案是使用跨平台的消息中间件ActiveMQ。

对于PHP客户端而言,要使用ActiveMQ,可以使用两种方式,一种是rest方式(基于HTTP),一种是借助Stomp:http://en.wikipedia.org/wiki/Streaming_Text_Orientated_Messaging_Protocol

基于rest是无状态的,而基于Stomp则要求保持连接(Stomp基于TCP协议)。

基于rest方式,可阅读如下链接,本文不做详解:http://activemq.apache.org/rest.html

下面具体示例PHP如何使用Stomp:

    //send a message to the queue 
    function sendMQ($data) { 
        $link = openMQ(); 
        foreach ($data as $pitem) { 
            //使用 persistent message 
            $result = stomp_send($link, $amq['queue'], $pitem, array("persistent" => "true")); 
            if (FALSE === $result) { 
                //do something 
            } 
        } 
    } 
     
    //receive message 
    function receiveMQ() { 
        $link = openMQ($queue); 
        stomp_subscribe($link, $queue); 
     
        while (1) { 
            if (TRUE === stomp_has_frame($link)) { 
                $frame = stomp_read_frame($link); 
     
                if (FALSE !== $frame) { 
                    stomp_ack($link, $frame['headers']['message-id']); 
                } else { 
                    //do something 
                    break; 
                } 
            } else { 
                break; 
            } 
        } 
        stomp_unsubscribe($link, $queue); 
        stomp_close($link); 
    } 
     
    //connection ActiveMQ 
    function openMQ(&$queue=FALSE) { 
        $amq = array( 
            'url' => 'tcp://127.0.0.1:61613', 
            'id' => 'xxx', 
            'pswd' => 'xxx', 
            'queue' => '/queue/mytest', 
            'enable' => TRUE 
        ); 
        $link = stomp_connect($amq['url'], $amq['id'], $amq['pswd']); 
        if (!$link) { 
            die("Can't connect MQ !!"); 
        } else { 
            return $link; 
        } 
    } 

注意Stomp读取方需要调整prefetchSize,如下:

stomp_subscribe($link, $queue, array("activemq.prefetchSize" => 1000)); 

否则可能会发现读取消息非常缓慢。prefetchSize字段含义如下:

 

Specifies the maximum number of pending messages that will be dispatched to the client.

Once this maximum is reached no more messages are dispatched until the client acknowledges a message.

Set to 1 for very fair distribution of messages across consumers where processing messages can be slow.

详细原因可仔细阅读:ActiveMQ In Action 一书。

推荐阅读:

Linux系统下ActiveMQ 安装 http://www.linuxidc.com/Linux/2012-03/55623.htm

Ubuntu下的ACTIVEMQ服务器 http://www.linuxidc.com/Linux/2008-07/14587.htm

Spring+JMS+ActiveMQ+Tomcat实现消息服务 http://www.linuxidc.com/Linux/2011-10/44632.htm

Linux环境下面ActiveMQ端口号设置和WEB端口号设置 http://www.linuxidc.com/Linux/2012-01/51100.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款