PHP AMQP Extensions App

Source: Internet
Author: User
Tags rabbitmq
PHP AMQP Extension App
Weekend break, idle to study the AMQP PHP extension, took a day to debug good

Advanced Message Queuing Protocol (AMQP) is an application-layer protocol specification used by asynchronous messaging. As a line-layer protocol, rather than an API (such as JMS), AMQP clients can send and receive information arbitrarily regardless of the source of the message. Now, quite a few servers and clients of different platforms can be put into use

My AMQP server is using RabbitMQ, RabbitMQ is installed on the Internet a lot.

The main point is PHP extension php-amqp, I use the latest amqp-1.0.1

The documents are old and new doped together so it took a long time to finish.

First, the process is CLIENT-AMQP server-client

The client on the left sends a message to the client on the right, the process:

1, get conection

2, Get Channel

3, define Exchange,queue

4, use a routingkey to binding the queue to an exchange

5, by specifying an exchange and a routingkey to send messages to the corresponding queue,

6, the receiver also acquires the connection at the time of receiving, then acquires the channel, then assigns a queue directly to the queue that it cares about, and it does not care about exchange,routingkey and how it is binding. Go to the corresponding queue and pick up the message, OK?

The following is the implementation of PHP:

Production message:
  ' localhost ', ' port ' = ' 5672 ', ' login ' = ' guest ', ' password ' = ' guest '; $conn = new Amqpconnection ($conn _args) if ($conn->connect ()) {echo "established a connection to the broker \ n";} else {echo "Cannot connect to the broker \ n";} Your message $message = Json_encode (Array (' Hello world! ', ' php ', ' C + + '));//Create Channel$channel = new Amqpchannel ($conn);// Create Exchange $ex = new Amqpexchange ($channel); $ex->setname (' Exchange ');//Create name $ex->settype (amqp_ex_type_direct) ; $ex->setflags (amqp_durable | Amqp_autodelete); echo "Exchange Status:". $ex->declare (); echo "\ n";//Create queue $q = new Amqpqueue ($channel);//Set queue name Add $q->setname (' queue ') if it does not exist; $q->setflags (amqp_durable | Amqp_autodelete); echo "Queue status:". $q->declare (); echo "\ n"; Echo ' queue bind: '. $q->bind (' Exchange ', ' Route.key ');//bind your queue to Routingkeyecho "\ n"; $channel->starttransaction (); echo "Send:". $ex->publish ($message, ' Route.key '); Send your message through the development Routingkey $channel->committransaction (); $conn->disconnECT ();? >


Consumer of the Receiving party
 
  ' localhost ', ' port ' = ' 5672 ', ' login ' = ' guest ', ' password ' = ' guest '                    , ' vhost ' + '/'); $conn = new Amqpco Nnection ($conn _args), $conn->connect (), $channel = new Amqpchannel ($conn); $q = new Amqpqueue ($channel); $q SetName (' queue2 '); $q->setflags (amqp_durable | Amqp_autodelete); echo "Queue status:". $q->declare (); echo "==========\n";     $messages = $q->get (amqp_autoack);p Rint_r ($messages->getbody ()); echo "\ n";//Disconnect$conn->disconnect ( );? >


I am a beginner of the message queue knowledge.
Don't know the pros and cons of Linux Message Queuing and AMQP
I use PHP's sysvmsg function to execute while loop can suspend on the server, listen to messages in real-time message queue, the message is taken, memory does not rise

In the AMQP while loop monitoring even if the non-message memory will be more and more high, and eventually lead to PHP using insufficient memory and error.

It seems to be a lot more exchange and learning.
  • Related Article

    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.