Basic concepts of phpamqp Message Queue RabbitMQ (2)

Source: Internet
Author: User
Tags rabbitmq
Connection: AMQPConnection first look at the server's existing link rabbitmqctl. The bat-qlist_connections is displayed as the air line below the code and then view the link connect. php? Php $ connectnewAMQPConnection (); $ connect-connect (); while (true) {} rabbitmqctl. bat-qlist_connections now serves

Connection: AMQPConnection first check the existing Server link rabbitmqctl. bat-q list_connections. It is displayed as null. Run the following code and check the link connect. php? Php $ connect = new AMQPConnection (); $ connect-connect (); while (true) {} rabbitmqctl. bat-q list_connections services

Connection: AMQPConnection

Check the existing connection of the server.

Rabbitmqctl. bat-q list_connections is blank



Run the following code and check the link.

Connect. php

 connect();while (true) {    }


Rabbitmqctl. bat-q list_connections current server Link



Channel: AMQPChannel

Rabbitmqctl. bat-q list_channels is blank


Run the Code channel. php.

 connect();$channel = new AMQPChannel($connect);while (true) {    }

Rabbitmqctl. bat-q list_channels:


Switch: Exchange

Rabbitmqctl. bat-q list_exchanges:


Run exchange. php

 connect();$channel = new AMQPChannel($connect);$exchange = new AMQPExchange($channel);$exchange->setName('exchange_name');$exchange->setType(AMQP_EX_TYPE_DIRECT);$exchange->declare();while (true) {    }

Rabbitmqctl. bat-q list_exchanges:


If the queue service is not restarted, The exchange_name switch will always exist.


Queue: AMQPQueue

Rabbitmqctl. bat-q list_queues:


Run queue. php In cmd.

 connect();$channel = new AMQPChannel($connect);$exchange = new AMQPExchange($channel);$exchange->setName('exchange_name');$exchange->setType(AMQP_EX_TYPE_DIRECT);$exchange->declare();$queue = new AMQPQueue($channel);$queue->setName('queue_name');$queue->declare();while (true) {    }

Run rabbitmqctl. bat-q list_queues.



Bind & routing_key

Rabbitmqctl. bat-q list_bindings:


Run the following php code bind. php:

 connect();$channel = new AMQPChannel($connect);$exchange = new AMQPExchange($channel);$exchange->setName('exchange_name');$exchange->setType(AMQP_EX_TYPE_DIRECT);$exchange->declare();$queue = new AMQPQueue($channel);$queue->setName('queue_name');$queue->declare();$queue->bind('exchange_name', 'routing_key');while (true) {    }

Run rabbitmqctl. bat-q list_bindings again.


Information: Envelope

Next we will change the above bind. php to an acceptor (Information processing end)

 connect();$channel = new AMQPChannel($connect);$exchange = new AMQPExchange($channel);$exchange->setName('exchange_name');$exchange->setType(AMQP_EX_TYPE_DIRECT);$exchange->declare();$queue = new AMQPQueue($channel);$queue->setName('queue_name');$queue->declare();$queue->bind('exchange_name', 'routing_key');while (true) {    $queue->consume('functionName');}function functionName($envelope,$queue) {    var_dump($envelope->getBody());}
Run the following in dos:


Let's write another sender, envelope. php.

 connect();$channel = new AMQPChannel($connect);$exchange = new AMQPExchange($channel);$exchange->setName('exchange_name');$exchange->setType(AMQP_EX_TYPE_DIRECT);$exchange->declare();$exchange->publish('hello world','routing_key');$connect->disconnect();

After running envelope. php, you can see that the receiving end has received the message.

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.