php-amqplib

How in RabbitMQ and PHP return task back to the queue?

北战南征 提交于 2021-01-27 17:59:18
问题 How can I return message back to the queue if processing result did not suit me. Found only information about message acknowledgments but I think that it does not suit me. I need that if as a result of processing I get the parameter RETRY message is added back to the queue. And then this worker or another one picks it up again and tries to process it. For example: <?php use PhpAmqpLib\Connection\AMQPStreamConnection; echo ' [*] Waiting for messages. To exit press CTRL+C', "\n"; $connection =

【rabbitmq-Php】-发布Publish 与订阅Subscribe

爷,独闯天下 提交于 2020-10-02 20:30:32
发布/订阅,使用扇型交换机(fanout) composer.json ### composer.json { "require": { "php-amqplib/php-amqplib": ">=2.9.0" } } 发布端(Publish) /** * rabbitmq * 发布/订阅 * Publish * https://github.com/rabbitmq/rabbitmq-tutorials * https://www.rabbitmq.com/tutorials/tutorial-three-php.html */ defined('DS') or define('DS', DIRECTORY_SEPARATOR); require_once __DIR__. DS . 'vendor' .DS.'autoload.php'; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPStreamConnection('192.168.0.83', 5672, 'admin', 'admin'); $channel = $connection->channel(); // 创建一个fanout类型的交换机

RabbitMQ 延迟队列-基于PHP实现

妖精的绣舞 提交于 2020-08-17 14:05:53
安装 RabbitMQ 延迟队列插件 RabbitMQ 延迟队列插件未安装直接使用的话,会报错: unknown exchange type 'x-delayed-message' 插件下载地址:https://www.rabbitmq.com/community-plugins.html 。下载 Erlang 可执行文件之后,复制到rabbit服务的插件目录(自己的安装目录,我的是 C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.14\plugins )中,然后开启插件服务: rabbitmq-plugins enable rabbitmq_delayed_message_exchange 测试步骤 创建测试项目的目录 mq 从 https://github.com/php-amqplib/php-amqplib 下载AMQP库(当然也可以通过 composer 安装,这里为了简单直接自己处理了),放入 mq 目录 编写 index.php,实现自动加载 创建 test 目录,里面分别创建 mqc.php 消费者和 mqp.php 生产者两个文件 跑脚本,测试消息的生产和消费: php -f index.php delayP p 3 生产消息,延时3秒 php -f index.php delayC c 消费消息 目录结构

RabbitMQ译文 — 远程过程调用(Remote procedure call (RPC))

☆樱花仙子☆ 提交于 2020-08-14 20:35:22
(使用 php-amqplib )   在 第二节教程 里,我们学习了怎样在多个工作者之间使用工作队列来分发耗时任务。   但如果我们需要在一个远程电脑上运行一个函数并且等待运行结果要怎样做呢?好吧,那是另一个问题了。这种模式通常被称为远程过程调用,或者RPC(Remote Procedure Call)。   在这篇教程里我们要使用RabbitMQ去建立一个RPC系统:一个客户端和一个可扩展的RPB服务器。因为我们还没有任何一个值得分发的耗时任务,我们就先建立一个仿真RPC服务用来返回“斐波纳契数列(Fibonacci numbers)”。 一、客户端接口(Client interface)    为了演示一个RPC服务怎样能被使用,我们先建立一个简单的客户端类。这个类将暴露一个命名为“call”的方法用于发送一个RPC请求并且阻塞程序执行直到应答被接收。 $fibonacci_rpc = new FibonacciRpcClient(); $response = $fibonacci_rpc->call( 30); echo ' [.] Got ', $response, "\n"; 关于RPC的注解 尽管RPC在计算机领域方面是一个很普通的方式,它却经常被批评。当一个程序员不清楚一个函数调用是否在本地或者是否这是一个缓慢的RPC的话,问题就来了

RabbitMQ的持久化

早过忘川 提交于 2020-04-06 00:04:06
RabbitMQ的持久化主要体现在三个方面,即交换机持久化,队列持久化及消息持久化 注意,因公司使用php-amqplib来实现RabbitMQ,故之后举例说明的代码均使用的php-amqplib,而非php的amqp扩展 1、交换机持久化 交换机的持久化其实就是相当于将交换机的属性在服务器内部保存,当MQ的服务器发生意外或关闭之后,重启RabbitMQ时不需要重新手动或执行代码去建立交换机,交换机会自动建立,相当于一直存在。 创建交换机的方法为exchange_declare($exhcange_name,$type,$passive,$durable,$auto_delete);,当$durable这个参数为true时,该交换机就会被存储到内存里,当RabbitMQ服务器重启时,会将该交换机自动重新创建,如果为false,重启后该交换机则会被从交换机队列里删掉。 2、队列持久化 队列持久化类似于交换机持久化,创建队列方法queue_declare中也有一个参数是$durable,也代表着RabbitMQ服务器重启时,是否自动创建队列,图中参数注释与方法中的参数(除队列名外)顺序一一对应 3、消息持久化 众所周知,RabbitMQ的消息是依附于队列存在的,所以想要消息持久化,那么前提是队列也要持久化。 消息的持久化与交换机持久化与队列持久化有所不同,消息的持久化在于创建消息的时候

php rabbitmq 库 php-amqplib 运行 demo

时光总嘲笑我的痴心妄想 提交于 2020-02-27 06:42:43
下载项目 php-amqplib 项目地址: https://github.com/php-amqplib/php-amqplib 下载项目: $ git clone https://github.com/php-amqplib/php-amqplib.git $ cd php-amqplib 注:需要安装好 composer( Linux 安装 php composer ) 更新依赖: $ composer update 时间可能比较长,请耐心等待。执行完成后,会出现 vender 目录。 测试 修改配置文件 tests/config.php ,把 HOST、PORT、USER、PASS、VHOST 这几项都改为自己 rabbitmq 的。 修改 demo/config.php ,关闭调试信息,注释掉: putenv('TEST_AMQP_DEBUG=1'); 发送消息: $ php amqp_publisher.php some text to publish 接收消息: php amqp_consumer.php 可以运行多个 consumer,实现消费负载均衡。 来源: oschina 链接: https://my.oschina.net/yogoup/blog/3166987

RabbitMQ - How to check if queue is empty?

本小妞迷上赌 提交于 2020-01-04 17:08:31
问题 I have a web service interface that abstracts a RabbitMQ server (don't ask me why, I know it's an unnecessary step, but I have to). That is, I poll messages from the queue through a web service call, not directly over amqp . Consuming via basic.consumer blocks the execution thread till there are messages in the queue. This makes the web service not return. Code for illustration: $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel();

PHP Amqp channel callback loop

梦想的初衷 提交于 2019-12-11 04:38:36
问题 The example code for RabbitMQ states Our code will block while our $channel has callbacks. Whenever we receive a message our $callback function will be passed the received message. With this code snippet while(count($channel->callbacks)) { $channel->wait(); } This confuses me, because the default timeout for PhpAmqpLib\Channel\AbstractChannel::wait is forever. public function wait($allowed_methods = null, $non_blocking = false, $timeout = 0) So if wait blocks forever, how would the code ever

Topic exchange ambiguity with RabbitMQ

谁说我不能喝 提交于 2019-12-07 18:25:08
问题 I'm a little confused. I'm trying to implement topic exchanges and am not sure what is needed. I want to have several routing keys and 1 topic exchange (the default amq.topic). My keys would be like: customer.appA.created customer.appB.created customer.*.created I want my queue(s) to be durable, but do I need 1 'customer' queue or 2 queues for appA and appB? I have my publisher figured out; connect, exchange declare, basic publish . But I'm struggling with the consumers. Let's say I want to

Topic exchange ambiguity with RabbitMQ

时光总嘲笑我的痴心妄想 提交于 2019-12-06 08:36:26
I'm a little confused. I'm trying to implement topic exchanges and am not sure what is needed. I want to have several routing keys and 1 topic exchange (the default amq.topic). My keys would be like: customer.appA.created customer.appB.created customer.*.created I want my queue(s) to be durable, but do I need 1 'customer' queue or 2 queues for appA and appB? I have my publisher figured out; connect, exchange declare, basic publish . But I'm struggling with the consumers. Let's say I want to open 3 consoles, one for each of the aforementioned routing keys. My current consumer has: connect,