2017 © Pedro Peláez
 

yii2-extension yii2-amqp

a consumer manage tool for amqp

image

hzted123/yii2-amqp

a consumer manage tool for amqp

  • Wednesday, May 10, 2017
  • by hzted123
  • Repository
  • 1 Watchers
  • 3 Stars
  • 3,301 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 6 Versions
  • 11 % Grown

The README.md

Yii2 amqp consumer tool

A tool to amqp consumer. It can declare exchange and queue with config. And Keep consumer alive with cronjobs., (*1)

This extension is based on this. Thanks webtoucher., (*2)

But with a few change:, (*3)

  • consumer setting by queue, not route or exchange
  • all exchange and queue setting in config
  • add listener-manage controller

Installation

The preferred way to install this extension is through composer., (*4)

Either run, (*5)

php composer.phar require --prefer-dist hzted123/yii2-amqp "*"

or add, (*6)

"hzted123/yii2-amqp": "*"

to the require section of your composer.json file., (*7)

Usage

Once the extension is installed, simply use it in your code by :, (*8)

Add the following in your console config:, (*9)

return [
    ...
    'components' => [
        ...
        'amqp' => [
            'class' => 'hzted123\amqp\components\Amqp',
            'host' => '******',
            'port' => 5672,
            'user' => '******',
            'password' => '******',
            'vhost' => '/',
            'exchange_configs' => [
                'exchange_name' => [
                    'options' => ['type' => 'topic', 'passive' => false, 'auto_delete' => false, 'durable' => true ],
                ],
                ... ...
            ],
            'queue_configs' => [
                'queue_name'    =>  [
                    'options' => ['passive' => false, 'auto_delete' => false, 'durable' => true, 'exclusive' => false],
                    'arguments' => ['x-max-length' => ['I', 1000000], 'x-max-length-bytes' => ['I', 300485760]],
                    'binds' => ['route' => 'exchange_name']
                ],
                ... ... 
            ],
        ],
    ],
    ...
    'controllerMap' => [
        'cron' => [
            'class' => 'mitalcoi\cronjobs\CronController',
            'interpreterPath' => '/usr/bin/php',
            'logsDir' => '/data/logs/cron',
            'logFileName' => '%L/php-%C.%A.%D.log',
            'bootstrapScript' => (dirname(dirname(__FILE__)) .'/yii',
            'cronJobs' =>[
                'listener-manage/keep' => [
                    'cron'      => '* * * * *',
                ]
            ],
        ],
        'listener' => [
            'class' => 'hzted123\amqp\controllers\AmqpListenerController',
            'interpreters' => [
                'queue_name' => '@app\components\DemoEventInterpreter', // interpreters for each queue
            ],
        ],
        'listener-manage' => [      //consumer keeper
            'class' => 'hzted123\amqp\controllers\ListenerManageController',
            'configs' => [
                ['queue' => 'queue_name', 'count' => 2]      // Keeping the number of consumers
            ]
        ],
    ],
];

Add messages interpreter class @app/components/DemoEventInterpreter with your handlers for different routing keys:, (*10)

<?php

namespace app\components;

use hzted123\amqp\components\AmqpInterpreter;


class DemoEventInterpreter extends AmqpInterpreter
{
    /**
     * Interprets AMQP message with routing key 'hello_world'.
     *
     * @param array $message
     */
    public function readHelloWorld($message)
    {
        // todo: write message handler
        $this->log(print_r($message, true));
    }
}

Usage

Just run command, (*11)

$ php yii listener-manage keep

to start all consumers , or like this, (*12)

* * * * * php yii listener-manage keep

Run command, (*13)

$ php yii listener-manage kill

to kill all consumers, when you deploy new code., (*14)

Run command, (*15)

$ php yii listener --queue=queue_name

to listen a queue on selected queue., (*16)

Also you can create controllers for your needs. Just use for your web controllers class hzted123\amqp\controllers\AmqpConsoleController instead of yii\web\Controller and for your console controllers class hzted123\amqp\controllers\AmqpConsoleController instead of yii\console\Controller. AMQP connection will be available with property connection. AMQP channel will be available with property channel., (*17)

Note: The configuration information, starting with version 1.0.2, exchange and route switching position, for multiple routes binding to a queue, (*18)

    'queue_configs' => [
        'queue_name'    =>  [
            'options' => ['passive' => false, 'auto_delete' => false, 'durable' => true, 'exclusive' => false],
            'arguments' => ['x-max-length' => ['I', 1000000], 'x-max-length-bytes' => ['I', 300485760]],
            'binds' => ['route' => 'exchange_name']
        ],
        ... ... 
    ],

The Versions

10/05 2017

dev-master

9999999-dev

a consumer manage tool for amqp

  Sources   Download

MIT

The Requires

 

by songming

extension yii2 amqp

10/05 2017

1.0.4

1.0.4.0

a consumer manage tool for amqp

  Sources   Download

MIT

The Requires

 

by songming

extension yii2 amqp

10/07 2016

1.0.3

1.0.3.0

a consumer manage tool for amqp

  Sources   Download

MIT

The Requires

 

by songming

extension yii2 amqp

10/07 2016

1.0.2

1.0.2.0

a consumer manage tool for amqp

  Sources   Download

MIT

The Requires

 

by songming

extension yii2 amqp

19/06 2016

1.0.1

1.0.1.0

a consumer manage tool for amqp

  Sources   Download

MIT

The Requires

 

by songming

extension yii2 amqp

04/06 2016

1.0.0

1.0.0.0

a consumer manage tool for amqp

  Sources   Download

BSD-3-Clause

The Requires

 

by songming

extension yii2