PHP AMQP Message Queuing RabbitMQ exchanger Type direct connection (iii) _php tutorial

Source: Internet
Author: User
Tags rabbitmq

1, Amqp_ex_type_direct: direct-Connect type

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
 
  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 side 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 ();

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
 
  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 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
for ($index = 1; $index < $index + +) {    $exchange->publish ($index, ' logs ');    Var_dump ("Send: $index");}

receive_two.php plus sleep (3)
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) on 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
$channel = new Amqpchannel ($connect);
Change to the following
$channel = new Amqpchannel ($connect); $channel->setprefetchcount (1);




http://www.bkjia.com/PHPjc/735888.html www.bkjia.com true http://www.bkjia.com/PHPjc/735888.html techarticle 1, Amqp_ex_type_direct: direct-connected direct type also includes: 1 pairs of 1 and 1 pairs n (n to 1, n to N) receive end receive.php code as follows connect (); $channel = new Amqpchannel ($ Connect); $exchange .

  • 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.