Home Backend Development PHP Tutorial 用PHP尝试RabbitMQ(amqp扩充)实现消息的发送和接收

用PHP尝试RabbitMQ(amqp扩充)实现消息的发送和接收

Jun 13, 2016 pm 12:15 PM
channel gt message name

用PHP尝试RabbitMQ(amqp扩展)实现消息的发送和接收

消费者:接收消息

逻辑:
创建连接-->创建channel-->创建交换机-->创建队列-->绑定交换机/队列/路由键-->接收消息

<?php /************************************** PHP amqp(RabbitMQ) Demo - consumer* Author: Linvo* Date: 2012/7/30*************************************///配置信息$conn_args = array(    'host' => '192.168.1.93',    'port' => '5672',    'login' => 'guest',    'password' => 'guest',    'vhost'=>'/');$e_name = 'e_linvo'; //交换机名$q_name = 'q_linvo'; //队列名$k_route = 'key_1'; //路由key//创建连接和channel$conn = new AMQPConnection($conn_args);if (!$conn->connect()) {    die("Cannot connect to the broker!\n");}$channel = new AMQPChannel($conn);//创建交换机$ex = new AMQPExchange($channel);$ex->setName($e_name);$ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型$ex->setFlags(AMQP_DURABLE); //持久化echo "Exchange Status:".$ex->declare()."\n";//创建队列$q = new AMQPQueue($channel);$q->setName($q_name);$q->setFlags(AMQP_DURABLE); //持久化echo "Message Total:".$q->declare()."\n";//绑定交换机与队列,并指定路由键echo 'Queue Bind: '.$q->bind($e_name, $k_route)."\n";//阻塞模式接收消息echo "Message:\n";while(True){    $q->consume('processMessage');    //$q->consume('processMessage', AMQP_AUTOACK); //自动ACK应答}$conn->disconnect();/*** 消费回调函数* 处理消息*/function processMessage($envelope, $queue) {    $msg = $envelope->getBody();    echo $msg."\n"; //处理消息    $queue->ack($envelope->getDeliveryTag()); //手动发送ACK应答}
Copy after login

生产者:发送消息

逻辑:
创建连接-->创建channel-->创建交换机对象-->发送消息

<?php /************************************** PHP amqp(RabbitMQ) Demo - publisher* Author: Linvo* Date: 2012/7/30*************************************///配置信息$conn_args = array(    'host' => '192.168.1.93',    'port' => '5672',    'login' => 'guest',    'password' => 'guest',    'vhost'=>'/');$e_name = 'e_linvo'; //交换机名//$q_name = 'q_linvo'; //无需队列名$k_route = 'key_1'; //路由key//创建连接和channel$conn = new AMQPConnection($conn_args);if (!$conn->connect()) {    die("Cannot connect to the broker!\n");}$channel = new AMQPChannel($conn);//消息内容$message = "TEST MESSAGE! 测试消息!";//创建交换机对象$ex = new AMQPExchange($channel);$ex->setName($e_name);//发送消息//$channel->startTransaction(); //开始事务for($i=0; $ipublish($message, $k_route)."\n";}//$channel->commitTransaction(); //提交事务$conn->disconnect();
Copy after login

需要注意的地方是:

queue对象有两个方法可用于取消息:consume和get。
前者是阻塞的,无消息时会被挂起,适合循环中使用;

后者则是非阻塞的,取消息时有则取,无则返回false。

测试截图

运行消费者:

1343873405_2469

运行生产者,发消息:

1343873408_3083

消费者接收到消息:

1343873412_6992

?

http://nonfu.me/p/9722.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

How to use element-plus to call message in vue3 How to use element-plus to call message in vue3 May 17, 2023 pm 03:52 PM

vue3 uses element-plus to call the message environment: vue3+typescript+element-plus1. After the global introduction of element, element has added the global method $message in app.config.globalProperties, so you can use mounted(){(thisasany) directly in the optionsAPI. $message.success("this.$message");}2. In the compositionAPI, the setup method passes in two variables props and

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

What does channel mean in Go language? What does channel mean in Go language? Dec 14, 2023 pm 02:21 PM

Channel in Go language is a mechanism for communication and data synchronization between coroutines. Can be thought of as a special data type, similar to a queue or pipe, used to transfer data between different coroutines. Channel provides two main operations: send and receive. Both send and receive operations in a channel are blocking, which means that if no sender or receiver is ready, the operation will be blocked until a coroutine is ready to perform the corresponding operation, etc.

How does golang function communicate with goroutine through channel? How does golang function communicate with goroutine through channel? May 01, 2024 pm 09:42 PM

Go language uses channels and goroutines to communicate. After creating the channel, the goroutine can pass

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

See all articles