PHP AMQP Message Queuing RabbitMQ exchanger Type Direct Connect (iii)

Source: Internet
Author: User
Tags rabbitmq

1, Amqp_ex_type_direct: direct-connected direct-connect type also includes: 1 pairs of 1 and 1 pairs n (n-to-1, n-N)

The Receive-side receive.php code is as follows?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <!--? php $connect =newamqpconnection ();   $connect--->connect ();   $channel =newamqpchannel ($connect); $exchange =newamqpexchange ($channel); $exchange->setname (' Exchange '); $exchange->settype (Amqp_ex_type_direct);   $exchange->declare (); $queue =newamqpqueue ($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 side send.php code is as follows
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!--? php $connect =newamqpconnection ();   $connect--->connect ();   $channel =newamqpchannel ($connect); $exchange =newamqpexchange ($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 ();

Run results




Create receive_one.php and receive_two.php and change the send.php code to code like this to allow us to watch receive_one.php and receive_two.php code the same or run multiple receivers with DOS
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <!--? php $connect =newamqpconnection ();   $connect--->connect ();   $channel =newamqpchannel ($connect); $exchange =newamqpexchange ($channel); $exchange->setname (' Exchange '); $exchange->settype (Amqp_ex_type_direct);   $exchange->declare (); $queue =newamqpqueue ($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
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <!--? php $connect =newamqpconnection ();   $connect--->connect ();   $channel =newamqpchannel ($connect); $exchange =newamqpexchange ($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 operation results are as follows

Queues will assign messages to each receive-side allocation process it seems perfect, but if you want to handle different tasks better, you need to Fair Dispatch
For example, when 1, 3 are processed by simple people 2, 4 are the complex task of processing if the task is too much receive_one.php is idle and receive_two.php is a heavy task we carried out the following test send.php changed to 5 50?
1 2 3 4 for ($index =1; $index <50; $index + +) {$exchange->publish ($index, ' logs '); Var_dump ("Send: $index"); }

receive_two.php plus sleep (3)
?
1 2 3) 4 5 function callback ($envelope, $queue) {var_dump ($envelope->getbody ());     Sleep (3); $queue->nack ($envelope->getdeliverytag ()); }

We run the program with the following results


Receive_one all run out and Receive_two only run one after Receive_one has been idle we can set $channel->setprefetchcount (1) at the receiving end;
The task does not receive new messages before they are completed send messages to other receivers
The following receive_one.php and receive_two.php?
1 $channel =newamqpchannel ($connect);
Change to the following
?
1 2 $channel =newamqpchannel ($connect); $channel->setprefetchcount (1);

PHP AMQP Message Queuing RabbitMQ exchanger Type Direct Connect (iii)

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.