activemq

文章介绍了如何使用ActiveMQ作为跨平台消息中间件,在PHP应用及ObjectiveC、Java等非PHP客户端间共享服务。通过两种方式接入ActiveMQ:基于HTTP的REST接口和基于TCP的Stomp协议。提供了详细的PHP客户端使用示例,包括发送和接收消息的方法,以及如何调整Stomp客户端的prefetchSize以提高消息读取效率。

假设你喜欢用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:

[php]  view plain copy
  1. //send a message to the queue    
  2. function sendMQ($data) {    
  3.     $link = openMQ();    
  4.     foreach ($data as $pitem) {    
  5.         //使用 persistent message    
  6.         $result = stomp_send($link$amq['queue'], $pitemarray("persistent" => "true"));    
  7.         if (FALSE === $result) {    
  8.             //do something    
  9.         }    
  10.     }    
  11. }    
  12.     
  13. //receive message    
  14. function receiveMQ() {    
  15.     $link = openMQ($queue);    
  16.     stomp_subscribe($link$queue);    
  17.     
  18.     while (1) {    
  19.         if (TRUE === stomp_has_frame($link)) {    
  20.             $frame = stomp_read_frame($link);    
  21.     
  22.             if (FALSE !== $frame) {    
  23.                 stomp_ack($link$frame['headers']['message-id']);    
  24.             } else {    
  25.                 //do something    
  26.                 break;    
  27.             }    
  28.         } else {    
  29.             break;    
  30.         }    
  31.     }    
  32.     stomp_unsubscribe($link$queue);    
  33.     stomp_close($link);    
  34. }    
  35.     
  36. //connection ActiveMQ    
  37. function openMQ(&$queue=FALSE) {    
  38.     $amq = array(    
  39.         'url' => 'tcp://127.0.0.1:61613',    
  40.         'id' => 'xxx',    
  41.         'pswd' => 'xxx',    
  42.         'queue' => '/queue/mytest',    
  43.         'enable' => TRUE    
  44.     );    
  45.     $link = stomp_connect($amq['url'], $amq['id'], $amq['pswd']);    
  46.     if (!$link) {    
  47.         die("Can't connect MQ !!");    
  48.     } else {    
  49.         return $link;    
  50.     }    
  51. }    

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

[php]  view plain copy
  1. stomp_subscribe($link$queuearray("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 一书。


参考资料:

Apache ActiveMQ - Enterprise messaging in actionstomp进阶说明-prefetchSize、ack header

ActiveMQ extensions to Stomp

stomping-with-php-intergration-with-activemq

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值