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