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']
],
... ...
],