xlabs / rabbitmqbundle

RabbitMQ 封装包

安装: 894

依赖: 10

建议: 0

安全: 0

类型:symfony-bundle

5.0.0 2024-03-18 16:59 UTC

This package is auto-updated.

Last update: 2024-09-18 18:17:35 UTC


README

一个用于Symfony2的RabbitMQ封装器。

安装

通过composer安装

php -d memory_limit=-1 composer.phar require xlabs/rabbitmqbundle

在您的AppKernel中

public function registerbundles()
{
    return [
    	...
    	...
    	new XLabs\RabbitMQBundle\XLabsRabbitMQBundle(),
    ];
}

配置示例

以下显示默认值

# app/config/config.yml
  
x_labs_rabbit_mq:
    host: 192.168.5.26
    port: 5672
    vhost: '/'
    user: admin
    password: admin
    exchange: gamification_exchange
    api_port: 15672
    queue_prefix: gamification_

用法

首先定义您的自定义生产者和其消费者

# YourBundle/Resources/config/services.yml
    ...
    my_custom_producer:
        class: YourBundle\YourFolder\Producer
        parent: xlabs_rmq_producer
        shared: false
    my_custom_consumer:
        class: YourBundle\YourFolder\Consumer
        tags:
            - { name: console.command }

您的生产者类

# YourBundle\YourFolder\Producer.php
  
namespace YourBundle\YourFolder;
  
use XLabs\RabbitMQBundle\RabbitMQ\Producer as Parent_Producer;
  
class Producer extends Parent_Producer
{
    public static function getQueueName()
    {
        // set your custom queue name for this producer here
        return 'my_custom_queue_name';
    }
  
    // if you need to modify the data sent in the message, create the following function
    public function _process($data)
    {
        // here you can deal with the data in the message if needed before being consumed
    }
}

您的消费者类

# YourBundle\YourFolder\Consumer.php
  
namespace YourBundle\YourFolder;
  
use XLabs\RabbitMQBundle\RabbitMQ\Consumer as Parent_Consumer;
  
class Consumer extends Parent_Consumer
{
    // set your custom consumer command name
    protected static $consumer = 'my_custom_name:bla_bla_bla';
    
    // set a TTL > 0 to automatically close the consumer after period
    // set TTL = 0 or dont set it to not close the consumer
    protected $ttl = 0; // seconds
  
    // following function is required as it is
    protected function configure()
    {
        $this
            ->setName(self::$consumer)
        ;
    }
  
    // following function is required as it is
    public function getQueueName()
    {
        return Producer::getQueueName();
    }
  
    public function callback($msg)
    {
        // this is all sample code to output something on screen 
        $container = $this->getApplication()->getKernel()->getContainer();
  
        $msg = json_decode($msg->body);
  
        echo "\n".$this->getQueueName()." - ".date('H:i:s d-m-Y')."\n";
        echo "--------------------------------------------\n";
        foreach($msg as $i => $tracker_item)
        {
            echo $i." : ".$tracker_item."\n";
        }
    }
}

此时您已经可以调用您的生产者

$container->get('my_custom_producer')->process(array(
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
));

并运行消费者

php app/console my_custom_name:bla_bla_bla

需求

确保添加 "management" 标签到 rabbitmq 用户,以便使用 API。

sudo rabbitmqctl set_user_tags USERNAME management