PHPackages                             hzted123/yii2-amqp - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. hzted123/yii2-amqp

ActiveYii2-extension[Queues &amp; Workers](/categories/queues)

hzted123/yii2-amqp
==================

a consumer manage tool for amqp

1.0.4(9y ago)33.9k1[1 issues](https://github.com/hzted123/yii2-amqp/issues)MITPHPPHP &gt;=5.3.0

Since Jun 4Pushed 9y ago1 watchersCompare

[ Source](https://github.com/hzted123/yii2-amqp)[ Packagist](https://packagist.org/packages/hzted123/yii2-amqp)[ RSS](/packages/hzted123-yii2-amqp/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (6)Used By (0)

Yii2 amqp consumer tool
=======================

[](#yii2-amqp-consumer-tool)

A tool to amqp consumer. It can declare exchange and queue with config. And Keep consumer alive with cronjobs.

This extension is based on [this](https://github.com/webtoucher/yii2-amqp). Thanks [webtoucher](https://github.com/webtoucher/).

But with a few change:

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

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

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

```

or add

```
"hzted123/yii2-amqp": "*"

```

to the require section of your `composer.json` file.

Usage
-----

[](#usage)

Once the extension is installed, simply use it in your code by :

Add the following in your console config:

```
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:

```
