innmind / amqp-bundle
此包已被放弃,不再维护。未建议替代包。
Symfony 扩展包,用于集成 AMQP 库
1.5.1
2017-11-28 07:21 UTC
Requires
- php: ~7.1
- innmind/amqp: ^1.2
- innmind/socket: ^1.1
- symfony/config: ~3.0
- symfony/console: ^3.0
- symfony/dependency-injection: ~3.0
- symfony/event-dispatcher: ^3.0
- symfony/framework-bundle: ^3.0
- symfony/http-kernel: ~3.0
- symfony/yaml: ~3.0
Requires (Dev)
- phpunit/phpunit: ~6.0
README
master |
develop |
---|---|
安装
composer require innmind/amqp-bundle
通过在您的项目 AppKernel.php
中添加以下行来启用扩展包:
class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Innmind\AMQPBundle\InnmindAMQPBundle, ); // ... } // ... }
然后,您需要声明您要使用的交换和队列
innmind_amqp: server: host: localhost # default port: 5672 # default user: guest # default password: guest # default vhost: / # default exchanges: urls: type: direct queues: crawler: consumer: my_consumer_service_id bindings: - exchange: urls queue: crawler
使用时,它将自动在 AMQP 服务器中创建交换、队列以及它们之间的绑定。
使用方法
为了发布一条新消息,您可以直接这样做:
use Innmind\AMQP\Model\Basic\Message\Generic; use Innmind\Immutable\Str; $container->get('innmind.amqp.producer.urls')(new Generic(new Str('http://example.com/')));
这将发布一个载荷为 http://example.com/
的消息到交换 urls
。
然后,为了消费消息,您可以通过代码使用 AMQP 客户端服务 innmind.amqp.client
,或者通过运行命令 innmind:amqp:get
或 innmind:amqp:consume
。这两个命令都接受队列名称作为第一个参数(在我们的例子中是 crawler
);innmind:amqp:consume
可以接受第二个参数来声明命令要消费的最大消息数。
示例
bin/console innmind:amqp:get crawler # process 1 message #or bin/console innmind:amqp:consume crawler # runs forever #or bin/console innmind:amqp:consume crawler 42 # process 42 messages