


php amqp message queue RabbitMQ exchange type direct connection (3)_PHP tutorial
1. AMQP_EX_TYPE_DIRECT: direct connection type
Direct connection types also include: 1 to 1 and 1 to N (N to 1, N to N)
The code of receive.php on the receiving end is as follows
<?php $connect = new AMQPConnection(); $connect->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 send.php code on the sending end is as follows
<?php $connect = new AMQPConnection(); $connect->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();
The running results are as shown in the figure


<?php $connect = new AMQPConnection(); $connect->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
<?php $connect = new AMQPConnection(); $connect->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 results are as follows

The queue will distribute the message to each receiving end for distribution and processing. This seems perfect, but if you want to better handle different tasks, you need fair scheduling
For example, when 1 and 3 deal with simple people and 2 and 4 deal with complex tasks, if there are too many tasks, receive_one.php will be idle and receive_two.php will have heavy tasks. We conduct the following tests Change send.php to 5 to 50
for ($index = 1; $index < 50; $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()); }
When we run the program, the results are as follows

After all receive_one runs, receive_two only runs one. After that, receive_one has been idle. We can set it on the receiving end by $channel->setPrefetchCount(1);
New messages will not be received until no one completes the task, and the message will be sent to other receivers.
As follows receive_one.php and receive_two.php
$channel = new AMQPChannel($connect);
$channel = new AMQPChannel($connect); $channel->setPrefetchCount(1);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to Use Swipe to Reply in iMessages on iPhone Note: The Swipe to Reply feature only works with iMessage conversations in iOS 17, not regular SMS conversations in the Messages app. Open the Messages app on your iPhone. Then, head to the iMessage conversation and simply swipe right on the iMessage you want to reply to. Once this is done, the selected iMessage will be in focus while all other messages will be blurred in the background. You'll see a text box for typing a reply and a "+" icon for accessing iMessage apps like Check-ins, Places, Stickers, Photos, and more. Just enter your message,

The message has been sent but rejected by the other party. This means that the sent information has been successfully sent from the device, but for some reason, the other party did not receive the message. More specifically, this is usually because the other party has set certain permissions or taken certain actions, which prevents your information from being received normally.

In iOS17, Apple has added several new features to its Messages app to make communicating with other Apple users more creative and fun. One of the features is the ability to use emojis as stickers. Stickers have been around in the Messages app for years, but so far, they haven't changed much. This is because in iOS17, Apple treats all standard emojis as stickers, allowing them to be used in the same way as actual stickers. This essentially means you're no longer limited to inserting them into conversations. Now you can also drag them anywhere on the message bubble. You can even stack them on top of each other to create little emoji scenes. The following steps show you how it works in iOS17

1. Being added to the blacklist: The message has been sent but rejected by the other party. Generally, you have been blacklisted. At this time, you will not be able to send messages to the other party, and the other party will not be able to receive your messages. 2. Network problems: If the recipient's network condition is poor or there is a network failure, the message may not be successfully received. At this point, you can try to wait for the network to return to normal before sending the message again. 3. The other party has set up Do Not Disturb: If the recipient has set up Do Not Disturb in WeChat, the sender’s messages will not be reminded or displayed within a certain period of time.

Xiaomi 14Pro is a flagship model with excellent performance and configuration. It has achieved high sales since its official release. Many small functions of Xiaomi 14Pro will be ignored by everyone. For example, it can be set to light up the screen for messages. Although the function is small, , but it is very practical. Everyone will encounter various problems when using the mobile phone. So how to set up the Xiaomi 14Pro to light up the screen for messages? How to set up Xiaomi Mi 14 Pro to light up the screen for messages? Step 1: Open your phone’s Settings app. Step 2: Swipe down until you find the "Lock screen and password" option and click to enter. Step 3: In the "Lock screen & passcode" menu, find and click the "Turn on screen for notifications" option. Step 4: On the "Turn on screen when receiving notifications" page, turn on the switch to enable

Application summary of queue technology in message delay and message retry in PHP and MySQL: With the continuous development of web applications, the demand for high concurrency processing and system reliability is getting higher and higher. As a solution, queue technology is widely used in PHP and MySQL to implement message delay and message retry functions. This article will introduce the application of queue technology in PHP and MySQL, including the basic principles of queues, methods of using queues to implement message delay, and methods of using queues to implement message retries, and give

Performance Analysis and Optimization Strategy of JavaQueue Queue Summary: Queue (Queue) is one of the commonly used data structures in Java and is widely used in various scenarios. This article will discuss the performance issues of JavaQueue queues from two aspects: performance analysis and optimization strategies, and give specific code examples. Introduction Queue is a first-in-first-out (FIFO) data structure that can be used to implement producer-consumer mode, thread pool task queue and other scenarios. Java provides a variety of queue implementations, such as Arr

The native Messages app on iPhone lets you easily edit sent texts. This way, you can correct your mistakes, punctuation, and even autocorrect wrong phrases/words that may have been applied to your text. In this article, we will learn how to edit messages on iPhone. How to Edit Messages on iPhone Required: iPhone running iOS16 or later. You can only edit iMessage text on the Messages app, and then only within 15 minutes of sending the original text. Non-iMessage text is not supported, so they cannot be retrieved or edited. Launch the Messages app on your iPhone. In Messages, select the conversation from which you want to edit the message
