Yii2 Queue
Extension for work with queues., (*1)
Installation
Use composer, (*2)
composer require "yiisolutions/yii2-migrations-advanced: *"
or add to composer.json require section:, (*3)
"yiisolutions/yii2-migrations-advanced": "*"
Configuration
First, we add to the configuration component., (*4)
<?php
return [
// ...
'components' => [
// ...
'queue' => [
'class' => 'yisolutions\queue\Queue',
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'password' => 'guest',
'vhost' => '/',
],
// ...
],
// ...
];
Send data
Next, to send data to the queue, we use the send() component method., (*5)
<?php
use yiisolutions\queue\Queue;
// ...
$data = ['foo' => 'bar'];
/** @var Queue $queue */
$queue = Yii::$app->get('queue');
$queue->send('queue.name', $data);
Receive data
We use the listen method to retrieve data., (*6)
<?php
use yiisolutions\queue\Queue;
use PhpAmqpLib\Message\AMQPMessage;
// ...
/** @var Queue $queue */
$queue = Yii::$app->get('queue');
$queue->listen('queue.name', function(AMQPMessage $msg) use ($queue) {
// process data
// acknowledgment message
$queue->acknowledgmentMessage($msg);
});