PHP code example of hzted123 / yii2-amqp
1. Go to this page and download the library: Download hzted123/yii2-amqp library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hzted123 / yii2-amqp example snippets
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
]
],
],
];
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));
}
}
'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']
],
... ...
],
php composer.phar
bash
$ php yii listener-manage keep
bash
* * * * * php yii listener-manage keep
bash
$ php yii listener-manage kill
bash
$ php yii listener --queue=queue_name