yii2-rabbitmq
Yii2 Rabbimmq, (*1)
Installation
add, (*2)
"cperdana/yii2-rabbitmq": "*"
to the require section of your composer.json file., (*3)
Used PhpAmqpLib
add component in config file, (*4)
'rabbit' => [
'class' => 'cperdana\rabbitmq\RabbitPhpAmqpLib',
'host' => '127.0.0.1',
'port' => 5666,
'user' => 'user_login',
'pass' => 'user_pass',
'vhost' => '/',
'readQueueName' => 'read_queue',
'sendExchangeName' => 'exchange_name',
'sendExchangeType' => 'fanout', // fanout/direct
'sendQueueName' => 'send_queue'
],
add console command, (*5)
use PhpAmqpLib\Message\AMQPMessage;
use cperdana\rabbitmq\RabbitPhpAmqpLib;
class MsgqueController extends \yii\console\Controller
{
private function execute($message){
echo $message->body;
$rabbit = \Yii::$app->rabbit;
$rabbit->sendAck($message);
}
public function actionSend($themsg){
$rabbit = \Yii::$app->rabbit;
$rabbit->send($themsg);
}
public function actionRead(){
$callback = function($message){
$this->execute($message);
};
$rabbit = \Yii::$app->rabbit;
$rabbit->read($callback);
}
}
as consumer, (*6)
php yii msgque/read
as publisher, (*7)
php yii msgque/send "this is the message to sent to rabbitmq"