Using ACTIVEMQ to implement Message Queuing in PHP

Source: Internet
Author: User
Tags ack autoload stomp

We've learned how to deploy ACTIVEMQ in the front,
We know that the task queue can be viewed through a management background in ACTIVEMQ.

Today


Using PHP to manipulate ACTIVEMQ, we can use a third-party extension.
Download:

require fusesource/stomp-php:2.0.*

Then create a new test.php:

<?phprequire __DIR__.‘/vendor/autoload.php‘; //引入自动加载的文件$connect = new \FuseSource\Stomp\Stomp(‘tcp://10.211.55.13/:61613‘);$connect->connect();$userId = 1001;$result = $connect->send(‘email‘,$userId); //比如发邮件var_dump($result);


Send message successfully, printbool(true)

We are in the ACTIVEMQ with the management background view, there is a queue called "email".

We can also send JSON data with an ID that we sent above.

$dataarray(‘id‘=>1001,‘email‘=>‘[email protected]‘,‘content‘=>‘test‘);$result$connect->send(‘email‘,json_encode($data

We can view the message details in MQ background

The above code is not perfect here. If our server restarts ACTIVEMQ, messages that are not processed are lost.
This time we need to use the send() third parameter of the method.

//消息持久化 persistent为true,字符串的‘true‘$result$connect->send(‘email‘,json_encode($data),array(‘persistent‘=>‘true‘));
We completed the "send" in front

Sends a message (an email message) to the MQ server.
And what about the tasks in the MQ queue?

<?Phprequire__dir__.'/vendor/autoload.php ';//introduction of automatically loaded files$connect = New \Fusesource\Stomp\Stomp (' tcp://10.211.55.13/:61613 ');$connect -Connect ();//Subscribe to queue messages$connect -Subscribe' Email ');if($connect -Hasframetoread ()) {$frame = $connect -Readframe (); Print_r ($frame);}

On the MQ service side, subscribe (listen) for queue messages.
On the server side is the command line execution: php mqServer.php
If there are no processed messages, you can read them and print the results as follows:

Fusesource\stomp\frame Object ([command] = MESSAGE [headers] = =Array([Expires] =0[Destination] =/queue/email [Priority] =4[Message-id] = id:localhost.localdomain-38488-1488196907415-3:2:-1:1:1[Timestamp] =1489477647931) [body] + = {"id":1001,"Email":"[email protected]","Content":"Test"})

bodyWe read the content we sent.

We loop through the read (dead Loop) waiting for new messages:

do{    if ($connect->hasFrameToRead()){        $frame=$connect->readFrame();        print_r($frame->body);    while (true);

After the message is processed (after the business is finished sending the message), notify MQ that I have processed the message.

    if ($connect->hasFrameToRead()){        $frame=$connect->readFrame();        //print_r($frame->body);        //做业务逻辑        //具体发送邮件的业务        //send email        //最后通知mq,我们已经处理了该条消息        $connect->ack($frame);    }

We can also optimize the code, solve the dead loop, control loop (here is a scenario demo)

 Do{//Will wait until a message is available to execute the following code    if($connect -Hasframetoread ()) {$frame = $connect -Readframe ();//print_r ($frame->body);        //Do business logic        //specific mail-sending business        //send EmailSleep2);//Analog delay        ///finally notify MQ that we have processed the message        $connect -Ack$frame); }//Control cycle    $next = true;if(File_exists (__dir__.'/stop ')){//If a file named Stop is        //It's not circulating.        $next = false; }} while($next);

Using ACTIVEMQ to implement Message Queuing in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.