alveos/swarrot
最新稳定版本:v4.20.0
Composer 安装命令:
composer require alveos/swarrot
包简介
A simple lib to consume RabbitMQ queues
README 文档
README
Swarrot is a PHP library to consume messages from any broker.
Installation
The recommended way to install Swarrot is through
Composer. Require the swarrot/swarrot package:
$ composer require swarrot/swarrot
Usage
Basic usage
First, you need to create a message provider to retrieve messages from your
broker. For example, with a PeclPackageMessageProvider (retrieves messages from
an AMQP broker with the pecl amqp package:
use Swarrot\Broker\MessageProvider\PeclPackageMessageProvider; // Create connection $connection = new \AMQPConnection(); $connection->connect(); $channel = new \AMQPChannel($connection); // Get the queue to consume $queue = new \AMQPQueue($channel); $queue->setName('global'); $messageProvider = new PeclPackageMessageProvider($queue);
Once it's done you need to create a Processor to process messages retrieved
from the broker. This processor must implement
Swarrot\Processor\ProcessorInterface. For example:
use Swarrot\Processor\ProcessorInterface; use Swarrot\Broker\Message; class Processor implements ProcessorInterface { public function process(Message $message, array $options): bool { echo sprintf("Consume message #%d\n", $message->getId()); return true; // Continue processing other messages } }
You now have a Swarrot\Broker\MessageProviderInterface to retrieve messages
and a Processor to process them. So, ask the Swarrot\Consumer to do its job :
use Swarrot\Consumer; $consumer = new Consumer($messageProvider, $processor); $consumer->consume();
Using a stack
Heavily inspired by stackphp/builder you
can use Swarrot\Processor\Stack\Builder to stack your processors.
Using the built in processors or by creating your
own, you can extend the behavior of your
base processor.
In this example, your processor is decorated by 2 other processors. The
ExceptionCatcherProcessor
which decorates your own with a try/catch block and the
MaxMessagesProcessor
which stops your worker when some messages have been consumed.
use Swarrot\Processor\ProcessorInterface; use Swarrot\Broker\Message; class Processor implements ProcessorInterface { public function process(Message $message, array $options): bool { echo sprintf("Consume message #%d\n", $message->getId()); return true; // Continue processing other messages } } $stack = (new \Swarrot\Processor\Stack\Builder()) ->push('Swarrot\Processor\MaxMessages\MaxMessagesProcessor', new Logger()) ->push('Swarrot\Processor\ExceptionCatcher\ExceptionCatcherProcessor') ->push('Swarrot\Processor\Ack\AckProcessor', $messageProvider) ; $processor = $stack->resolve(new Processor());
Here is an illustration to show you what happens when this order is used:
Processors
Official processors
- AckProcessor
- Doctrine related processors (thanks to Adrien Brault)
- ExceptionCatcherProcessor
- InsomniacProcessor (thanks to Adrien Brault)
- InstantRetryProcessor
- MaxExecutionTimeProcessor (thanks to Remy Lemeunier)
- MaxMessagesProcessor (thanks to Remy Lemeunier)
- MemoryLimitProcessor (thanks to Christophe Coevoet)
- RetryProcessor
- ServicesResetterProcessor (thanks to Pierrick Vignand)
- SignalHandlerProcessor
- XDeathMaxCountProcessor (thanks to Anthony Moutte)
- XDeathMaxLifetimeProcessor (thanks to Anthony Moutte)
Create your own processor
To create your own processor and be able to use it with the StackProcessor, you
just need to implement ProcessorInterface and to take another
ProcessorInterface as first argument in constructor.
Deprecated processors & message providers / publishers
In order to reduce swarrot/swarrot dependencies & ease the maintenance, some
processors & message providers / publishers have been deprecated in 3.x version.
They will be deleted in 4.0.
If you use those deprecated classes you could create your own repository to keep them or we could create a dedicated repository under the swarrot organisation if you're willing to help to maintain them.
Message providers / publishers
- SQS Message provider (in 3.5.0)
- Stomp message providers (in 3.6.0)
- Stomp message publishers (in 3.7.0)
- Interop message publishers & providers (in 3.7.0)
Processors
- SentryProcessor (in 3.5.0)
- RPC related processors (in 3.5.0)
- NewRelicProcessor (in 3.7.0)
Inspiration
License
Swarrot is released under the MIT License. See the bundled LICENSE file for details.
alveos/swarrot 适用场景与选型建议
alveos/swarrot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.99k 次下载、GitHub Stars 达 365, 最近一次更新时间为 2015 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「AMQP」 「worker」 「swarrot」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alveos/swarrot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alveos/swarrot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alveos/swarrot 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Queue
Work with RabbitMQ in Laravel.
PHP AMQP Binding Library
SlimQ Enables You to push jobs to background workers
The Listener component of the RabbitEvents library.
A Laravel package to monitor queue jobs.
统计信息
- 总下载量: 1.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 365
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-03-09