wowo / wowo-queue-bundle
WowoQueueBundle 提供了统一的方法来使用队列系统,如 Beanstalkd、RabbitMQ、平面文件、数据库驱动队列等。
Requires
- pda/pheanstalk: v2.1.0
Requires (Dev)
- aws/aws-sdk-php: 2.*
- mockery/mockery: 0.7.2
This package is not auto-updated.
Last update: 2024-09-28 13:30:42 UTC
README
WowoQueueBundle 提供了统一的方法来使用队列系统,如 Beanstalkd、RabbitMQ、平面文件、数据库驱动队列等。目前它只支持 Beanstalkd,但您可以添加自己的实现并发送 pull 请求。
安装
步骤 1:下载 WowoQueueBundle
如果您正在使用 Deps(Symfony 2.0.x)
将以下行添加到您的 deps 文件中
[WowoQueueBundle]
git=git://github.com/wowo/WowoQueueBundle.git
target=bundles/Wowo/QueueBundle
[pheanstalk]
git=https://github.com/pda/pheanstalk
target=/pheanstalk
version=v2.1.0
现在,运行 vendors 脚本来下载包
$ php bin/vendors install
如果您正在使用 Composer(Symfony >= 2.1.x)
将以下行添加到您的 composer.json 的 requirements 部分
"require": { "wowo/wowo-queue-bundle": "dev-master" }
现在,使用 composer 安装包
$ php composer.phar install
步骤 2:配置自动加载器
(如果您使用 composer,可以直接跳到步骤 3)
将 Wowo 命名空间添加到自动加载器中
<?php // app/autoload.php $loader->registerNamespaces(array( // ... 'Wowo' => __DIR__.'/../vendor/bundles', ));
同时在自动加载的底部添加 Pheanstalk 初始化
// ... require_once __DIR__.'/../vendor/pheanstalk/pheanstalk_init.php';
步骤 3:启用包
最后,在 kernel 中启用包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Wowo\QueueBundle\WowoQueueBundle(), ); }
步骤 4:安装和运行 beanstalkd
在 Debian Linux 系统(包括 Ubuntu)上,您可以运行
$ sudo apt-get install beanstalkd
然后将其作为守护进程运行
$ beanstalkd -d -l 127.0.0.1 -p 11300
注意: 如果您的 beanstalkd 服务运行在其他地址或端口上,您必须在配置中设置以下参数
parameters: wowo_queue.default.address: 127.0.0.1:11300
别忘了将 127.0.0.1:11300 替换为您的地址和端口。
用法
使用 Beanstalkd 内存队列服务器
从控制器获取带有 Beanstalkd 实现的管理器
$manager = $this->get('wowo_queue.manager');
将一些工作放入默认通道 wowo_queue。这可以通过 wowo_queue.pheanstalk.tube 参数进行更改
$manager->put(json_encode(array('payload' => 'Hello world', 'date' => new \DateTime())));
一旦工作出现在队列中,就获取工作。以下示例展示了无限循环。此类操作应作为后台任务运行,侦听队列并在出现工作时处理工作。
while ($job = $manager->get()) { $data = json_decode($job->getData(), true); printf("Job date: %s, payload: %s\n", $data['date']['date'], $data['payload']); }
从队列中删除工作相当简单
$job = $manager->get(); $manager->delete($job);
在不使用服务容器的情况下使用 Beanstalkd 实现
创建管理器对象很简单,可以按照以下方式进行。这在您不使用 bundle 在 Symfony 项目中很有用。
$implementation = new Wowo\QueueBundle\Implementation\BeanstalkdQueueImplementation(); $implementation->configure(array( 'address' => 'localhost', 'tube' => 'foo-tube', )); $manager = new Wowo\QueueBundle\QueueManager($implementation);
如果您没有使用自动加载器,Pheanstalk 需要手动自动加载
require_once('pheanstalk/classes/pheanstalk/classloader.php'); \pheanstalk_classloader::register('pheanstalk/classes/');
Amazon SQS
SQSQueueImplementation 利用 AWS PHP SDK v2
配置
要将 SQS 实现注入到您的 QueueManager 中,请确保使用配置好的 SQS 客户端和将通道名称映射到 SQS 队列名称的关联数组实例化它。
Symfony 包的 services.yml 示例
# general AWS SQS client
queue.sqs_client:
class: Aws\Sqs\SqsClient
factory_class: Aws\Sqs\SqsClient
factory_method: factory
arguments:
- {credentials: {key: %amazon.s3.key%, secret: %amazon.s3.secret%}, region: %amazon.s3.region%}
# wowo sqs implementation
queue.implementation.sqs:
class: Wowo\QueueBundle\Implementation\SQSQueueImplementation
arguments: [@queue.sqs_client, %queue.tubes_to_sqs%]
从那里,您可以直接将 SQS 实现注入到您的 QueueManager 中以获取或放置消息