Suppose you like to use PHP to build Web applications, and then there are some peripheral applications, including mobile terminal apps and mobile Web,
Because mobile apps and other non-PHP languages such as objectivec and Java are compiled, and you want to share some basic services between these client applications,
We hope these basic services can be provided in a loosely coupled, highly scalable, and high-performance manner. A better solution is to use the cross-platform message middleware activemq.
For PHP clients, activemq can be used in two ways: Rest (based on HTTP) and stomp:
Http://en.wikipedia.org/wiki/Streaming_Text_Orientated_Messaging_Protocol
Rest-based is stateless, while stomp-based connection is required (stomp is based on the TCP protocol ).
Based on the rest method, you can read the following link, which is not described in this article:
Http://activemq.apache.org/rest.html
The following is an example of how PHP uses stomp:
// Send a message to the queue function sendmq ($ data) {$ link = openmq (); foreach ($ data as $ pitem) {// use persistent message $ result = stomp_send ($ link, $ AMQ ['queue '], $ pitem, array ("persistent" => "true ")); if (false ===$ result) {// do something }}// receive message function implements EMQ () {$ 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 ;}}
Note that the stomp reader must adjust the prefetchsize as follows:
stomp_subscribe($link, $queue, array("activemq.prefetchSize" => 1000));
Otherwise, you may find that reading messages is very slow. The meaning of the prefetchsize field is as follows:
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 consumer SS consumers where processing messages can be slow.
For more information, see activemq in action.
References:
Apache activemq-enterprise messaging in actionStomp advanced description-prefetchsize, Ack Header
Activemq extensions to stomp
Stomping-with-PHP-intergration-with-activemq