trt / async-tasks-bundle
此包允许在symfony2中分发异步事件。它使用RabbitMQ向消费者发送消息。
dev-master
2014-10-15 18:58 UTC
Requires
- php: >=5.3.0
- symfony/monolog-bundle: ~2.3
- symfony/symfony: ~2.3
- videlalvaro/php-amqplib: >=2.1.0
Requires (Dev)
- phpunit/phpunit: >=3.7.27
This package is not auto-updated.
Last update: 2024-09-24 02:34:06 UTC
README
关于
AsyncTasksBundle允许通过RabbitMq(使用php-amqplib库)在您的Symfony2应用程序中发送异步消息。
发布
消息发布非常简单。
- 创建一个异步事件
Trt\AsyncTasksBundle\Event\AsyncEvent
- 分发它!
- 完成
$event = new AsyncEvent( 'async_event_name', array('date'=> (new \DateTime())->format('d-m-Y H:s') ) ); $this->get('event_dispatcher')->dispatch($event->getName(), $event);
消费
现在,如果您想消费消息
- 编写您自己的领域特定服务,实现
Trt\AsyncTasksBundle\Listener\ListenerInterface
接口。 - 给服务打上标签
{name: trt_async.listener.listen, event: async_event_name}
,其中event键是事件名称。 - 运行
$ app/console trt:async:run async_event_name
所有示例都假定有一个正在运行的RabbitMQ服务器。
安装
这些说明已在用Symfony2 Standard 2.3.4创建的项目上进行了测试。
将AsyncTasksBundle添加到您的composer.json中,并运行composer install。
"require": { "php": ">=5.3.3", .... "trt/async-tasks-bundle": "dev-master", ... },
将AsyncTasksBundle添加到您的应用程序的kernel中
public function registerBundles() { $bundles = array( ... new Trt\AsyncTasksBundle\TrtAsyncTasksBundle(), ... ); ... }
配置
trt_async_tasks: # This prefix allows the dispatcher to detect async event, # if you want use another prefix put here event: prefix: async_ mq: # The rabbitMq host / port connection_params: host: %mq_host% port: %mq_port% # Define the exchange name for rabbitmq exchange: exchange: 'exchange_symfony_events'
命名约定
AsyncTasksBundle的事件检测机制基于命名约定,事件名称将与队列名称相同。每个包含“async”字符串名称的事件都将被检测为AysncEvent。
名称前缀可以通过配置覆盖
trt_async_tasks: event: prefix: acme_async
此包将在2013年的SymfonyDayIt(罗马)上展出
分叉它并贡献以解决问题 :-)
许可协议
见:resources/meta/LICENSE.md
致谢
此包的结构和文档部分基于SonataNotificationBundle