texthtml/pimple-rabbitmq-provider

Silex服务提供商,用于RabbitMQ。将php-amqplib/rabbitmq-bundle集成到Silex中。

v1.0.0 2016-12-05 09:11 UTC

This package is auto-updated.

Last update: 2024-09-24 19:24:11 UTC


README

关于

此Pimple服务提供商将出色的RabbitMqBundle集成到您的应用程序中。安装由Alvaro Videla创建的此包后,您可以使用RabbitMQ消息功能,使用php-amqplib库。

安装此服务提供商后,从控制器发送消息将类似于

$app->post('/message', function(Request $request) use ($app){
    $producer = $app['rabbit.producer']['my_exchange_name'];
    $producer->publish('Some message');

    return new Response($msg_body);
});

稍后,当您想从名为'my_queue'的队列中消费50条消息时,只需在CLI上运行

$ ./app/console rabbitmq:consumer -m 50 my_queue

要了解您可以使用此包做什么,请阅读包的README

安装

使用Composer要求库

$ composer require texthtml/pimple-rabbitmq-provider

然后,为了激活服务,在创建Pimple容器后注册服务提供商。在Silex 2中

use Silex\Application;
use TH\RabbitmqProvider\RabbitServiceProvider;

$app = new Application();
$app->register(new RabbitServiceProvider());

开始发送消息 ;)

用法

在来自Symfony包的README文件中,您可以查看所有可用的选项。例如,要使用两个不同连接、一对生产者和一个消费者配置我们的服务,我们将传递以下配置

$app->register(new RabbitServiceProvider(), [
    'rabbit.connections' => [
        'default' => [
            'host'      => 'localhost',
            'port'      => 5672,
            'user'      => 'guest',
            'password'  => 'guest',
            'vhost'     => '/'
        ],
        'another' => [
            'host'      => 'another_host',
            'port'      => 5672,
            'user'      => 'guest',
            'password'  => 'guest',
            'vhost'     => '/'
        ]
    ],
    'rabbit.producers' => [
        'first_producer' => [
            'connection'        => 'another',
            'exchange_options'  => ['name' => 'a_exchange', 'type' => 'topic']
        ],
        'second_producer' => [
            'connection'        => 'default',
            'exchange_options'  => ['name' => 'a_exchange', 'type' => 'topic']
        ],
    ],
    'rabbit.consumers' => [
        'a_consumer' => [
            'connection'        => 'default',
            'exchange_options'  => ['name' => 'a_exchange','type' => 'topic'],
            'queue_options'     => ['name' => 'a_queue', 'routing_keys' => ['foo.#']],
            'callback'          => 'your_consumer_service'
        ]
    ]
]);

请注意,消费者中选择的回调需要在Pimple容器中注册过的服务。消费者服务实现ConsumerInterface,该接口有一个public的execute()方法。

致谢