Php amqp Message Queue RabbitMQ switch direct connection (3)

Source: Internet
Author: User
Tags rabbitmq

1. AMQP_EX_TYPE_DIRECT: the direct connection type includes: 1-to-1 and 1-to-N (N-to-1 and N-to-N)

The receive. php code at the receiving end is as follows:

 connect();$channel = new AMQPChannel($connect);$exchange = new AMQPExchange($channel);$exchange->setName('exchange');$exchange->setType(AMQP_EX_TYPE_DIRECT);$exchange->declare();$queue = new AMQPQueue($channel);$queue->setName('logs');$queue->declare();$queue->bind('exchange', 'logs');while (true) {    $queue->consume('callback');}$connection->close();function callback($envelope, $queue) {    var_dump($envelope->getBody());    $queue->nack($envelope->getDeliveryTag());}

The sending terminal send. php code is as follows:
 connect();$channel = new AMQPChannel($connect);$exchange = new AMQPExchange($channel);$exchange->setName('exchange');$exchange->setType(AMQP_EX_TYPE_DIRECT);$exchange->declare();$exchange->publish('direct type test','logs');var_dump("Send Message OK");$connect->disconnect();

Running result




Create receive_one.php and receive_two.php, and change the send. php code to the following code to make it easier for us to watch that the code of receive_one.php and receive_two.php are the same or multiple receivers run with dos
 connect();$channel = new AMQPChannel($connect);$exchange = new AMQPExchange($channel);$exchange->setName('exchange');$exchange->setType(AMQP_EX_TYPE_DIRECT);$exchange->declare();$queue = new AMQPQueue($channel);$queue->setName('logs');@$queue->declare();$queue->bind('exchange', 'logs');while (true) {    $queue->consume('callback');}$connection->close();function callback($envelope, $queue) {    var_dump($envelope->getBody());    $queue->nack($envelope->getDeliveryTag());}




Send. php
 connect();$channel = new AMQPChannel($connect);$exchange = new AMQPExchange($channel);$exchange->setName('exchange');$exchange->setType(AMQP_EX_TYPE_DIRECT);$exchange->declare();for ($index = 1; $index < 5; $index++) {    $exchange->publish($index,'logs');    var_dump("Send:$index");}$exchange->delete();$connect->disconnect();

The running result is as follows:

The queue will distribute messages to each receiver for processing. This seems perfect, but if you want to better process different tasks, you need Fair Scheduling
For example, when 1 and 3 process simple tasks, 2 and 4 Process complicated tasks. If too many tasks exist, receive_one.php is idle, and receive_two.php is heavy, we will perform the following test to send. change php to 5 to 50
for ($index = 1; $index < 50; $index++) {    $exchange->publish($index,'logs');    var_dump("Send:$index");}

Receive_two.php with sleep (3)
function callback($envelope, $queue) {    var_dump($envelope->getBody());    sleep(3);    $queue->nack($envelope->getDeliveryTag());}

The result of running the program is as follows:


After all receive_one is run and receive_two is run, receive_one remains idle. We can set $ channel-> setPrefetchCount (1) on the acceptor );
Do not receive new messages before the task is completed. Send the messages to other receivers.
Receive_one.php and receive_two.php
$channel = new AMQPChannel($connect);
Changed to the following:
$channel = new AMQPChannel($connect);$channel->setPrefetchCount(1);




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.