mouf/oo-amqp-client
Composer 安装命令:
composer require mouf/oo-amqp-client
包简介
An object oriented wrapper on top of php-amqplib helping work with RabbitMQ in a more object oriented way.
README 文档
README
About Object Oriented AMQP Client
This package contains an object oriented wrapper on top of php-amqplib helping work with RabbitMQ in a more object oriented way.
Using this package, exchanges, bindings and queues are represented as objects. This is useful, especially if you want to inject those objects in your dependency injection container.
Installation
composer require mouf/oo-amqp-client
Usage
Before using this library, you should be accustomed to the AMQP concepts. If you are not, we strongly advise you to start reading the "AMQP 0-9-1 Model Explained" document from the RabbitMQ documentation.
Done? Let's get started.
Creating a client
The first thing you want to create is a Client object. A Client represents a connection to RabbitMQ (for those of you used to php-amqplib, it is both a connection AND a channel).
use Mouf\AmqpClient\Client; $client = new Client( $rabbitmq_host, $rabbitmq_port, $rabbitmq_user, $rabbitmq_password, $rabbitmq_vhost = '/', $rabbitmq_insist = false, $rabbitmq_login_method = 'AMQPLAIN', $rabbitmq_login_response = null, $rabbitmq_locale = 'en_US', $rabbitmq_connection_timeout = 3.0, $rabbitmq_read_write_timeout = 3.0, $rabbitmq_context = null, $rabbitmq_keepalive = false, $rabbitmq_heartbeat = 0 );
Note: the Client class exposes a number of useful configuration methods (you do not need to use those if you don't know what they do):
public function setPrefetchSize($prefetchSize); public function setPrefetchCount($prefetchCount); public function setAGlobal($aGlobal);
Creating an exchange
In AMQP, exchanges are the objects that receive messages and are in charge of forwarding those messages to queues.
You must therefore define an Exchange objects to send messages.
use Mouf\AmqpClient\Objects\Exchange; $exchange = new Exchange($client, 'exchange_name', 'fanout');
When creating an exchange, you pass to the constructor the Client object, the exchange name, and the exchange type.
Note: the exchange will self-register in the client.
You can apply advanced configuration using configuration methods:
public function setPassive($passive); public function setDurable($durable); public function setAutoDelete($autoDelete); public function setInternal($internal); public function setNowait($nowait); public function setArguments($arguments); public function setTicket($ticket);
Creating a queue and a binding
Messages arriving to an exchange are forwarded to a queue through a binding.
We will now create a queue to store our messages.
use Mouf\AmqpClient\Objects\Queue; $queue = new Queue($client, 'queue_name', [ new Consumer(function(AMQPMessage $msg) { // Do some stuff with the received message }) ]);
When creating a client, you pass to the constructor the Client object, the client name, and an array of Consumer objects (actually an array of objects implementing the ConsumerInterface).
A Consumer object is an object that contains code that will be called each time a message is received.
Note: the queue will self-register in the client.
You can apply advanced configuration to your queue using those configuration methods:
public function setPassive($passive); public function setDurable($durable); public function setExclusive($exclusive); public function setAutoDelete($autoDelete); public function setNoWait($noWait); public function setArguments($arguments); public function setTicket($ticket); public function setDeadLetterExchange(Exchange $exchange); public function setConfirm($confirm); public function setConsumerCancelNotify(Queue $consumerCancelNotify); public function setAlternateExchange(Queue $alternateExchange); public function setTtl($ttl); public function setMaxLength($maxLength); public function setMaxPriority($maxPriority);
You will certainly want to use the setDurable method if you want your queue to store messages in case of outage of the receiver.
At this point, we have an exchange, we have a queue, but both are not linked together. We need to bind those, using a Binding object.
use Mouf\AmqpClient\Objects\Binding; $binding = new Binding($exchange, $queue); $client->register($binding);
A Binding links an exchange to a queue.
Important: unlike the Exchange and the Queue, a Binding does not self-register in the client. You have to declare it in the client yourself, using the Client::register method.
Done? Let's send and receive messages!
Sending a message
In order to send a message, you simply use the Exchange::publish method:
$exchange->publish(new Message('your message body'), 'message_key'); // ... and that's it!
You may still want to configure a bit more the sending of your message. The Exchange::publish method accepts a number of optional arguments:
public function publish(Message $message, string $routingKey, bool $mandatory = false, bool $immediate = false, $ticket = null);
Also, the Message class can be tweaked with one of those methods:
public function setContentType(string $content_type); public function setContentEncoding(string $content_encoding); public function setApplicationHeaders(array $application_headers); public function setDeliveryMode(int $delivery_mode); public function setPriority(int $priority); public function setCorrelationId(string $correlation_id); public function setReplyTo(string $reply_to); public function setExpiration(string $expiration); public function setMessageId(string $message_id); public function setTimestamp(\DateTimeInterface $timestamp); public function setType(string $type); public function setUserId(string $user_id); public function setAppId(string $app_id); public function setClusterId(string $cluster_id);
Receiving messages
As we already saw, the first step to receiving message is creating a queue and adding Consumer objects to that queue.
We still need to tell PHP to start listening, otherwise, the callbacks in the Consumer will never be called.
This can be done using the ConsumerService class.
$consumerService = new ConsumerService($client, [ $queue ]); $consumerService->run();
The ConsumerService constructor takes the client in parameter, and the array of queues that must be listened to.
The ConsumerService::run method will start listening on arriving messages, in an infinite loop.
Notice that you can use $consumerService->run(true); if you want to listen to one message only and return afterward.
Acknowledgements and error handling
When you receive a message, an acknowledgement will not be sent before the Consumer has finished consuming the message.
If an exception is triggered in the Consumer, a nack will be sent instead to RabbitMQ.
Note: if your consumer callback throws an exception implementing the RetryableExceptionInterface interface, the nack message will be sent with the "requeue" flag. The message will be requeued.
Note: if your consumer callback throws an exception implementing the FatalExceptionInterface interface, the exception will be propagated by the consumer (hence leading to the crash of the consumer script). Otherwise, consumer will continue processing messages.
Exceptions are logged by default using the error_log function. You can override this behaviour by passing a PSR-3 compliant logger to the AbstractConsumer constructor.
Writing your consumer as a class
So far, to create a consumer, we used the Consumer class that takes a callback as first constructor parameter.
As an alternative, you can extend the AbstractConsumer class and implement the onMessageReceived method:
class MyConsumer extends AbstractConsumer { public function onMessageReceived($msg) { // Do some stuff. } }
Sending a message to a given queue
If you want to target a special queue and send a message to it directly, you have 2 options.
Option 1: create a DefaultExchange object and pass the queue name as the key of the message.
use Mouf\AmqpClient\Objects\DefaultExchange; $exchange = new DefaultExchange($client); // Simply pass the queue name as the second parameter of "publish". // Note: you do not need to bind the queue to the exchange. RabbitMQ does this automatically. $exchange->publish(new Message('your message body'), 'name_of_the_target_queue'); // ... and that's it!
Option 2: use the publish method of the Queue object:
use Mouf\AmqpClient\Objects\Queue; $queue = new Queue($client, 'queue_name', [ new Consumer(function(AMQPMessage $msg) { // Do some stuff with the received message }) ]); // Shazam! We are directly sending a message to the queue. No exchange needed! $queue->publish(new Message('your message body'));
Note: these are RabbitMQ specific features and might not work with other AMQP buses.
Symfony console integration
This package comes with 2 Symfony commands that you can use to send and receive messages.
Mouf\AmqpClient\Commands\PublishCommand(amqp:publish) allows you to send an arbitrary message on an exchange (read from a file or from STDIN)Mouf\AmqpClient\Commands\ConsumeCommand(amqp:consume) listen to all configured queues
Running the unit tests
This package uses PHPUnit for unit tests.
To run the tests:
vendor/bin/phpunit
Obviously, you need a running RabbitMQ server to test this package. If you use Docker, you can start one using:
docker run -p 5672:5672 -p 15672:15672 rabbitmq:management
mouf/oo-amqp-client 适用场景与选型建议
mouf/oo-amqp-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 172.2k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 07 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「hydrator」 「TDBM」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mouf/oo-amqp-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mouf/oo-amqp-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mouf/oo-amqp-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Symfony bundle for TDBM.
A Symfony bundle for thecodingmachine/tdbm-graphql.
A PHP hydrator allowing easy mapping between an array and an object.
Hydrator to map or extract an extract object
A simple library that uses the hydration pattern to fill objects given different sources of data.
Doctrine ORM hydrators for Laminas
统计信息
- 总下载量: 172.2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 39
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-07