1. 安裝:php
執行命令sql
php composer.phar require --prefer-dist yiisoft/yii2-queue
或者添加
"yiisoft/yii2-queue": "~2.1"
到 composer.json
文件數據庫
2.配置:json
yii2-queue 可選驅動有 DB, Redis, RabbitMQ, AMQP, Beanstalk and Gearman. 也可配置成 syncyii2
DB 隊列:app
首先配置 consolecomposer
'controllerMap' => [ // ... 'migrate' => [ 'class' => yii\console\controllers\MigrateController::class, 'migrationPath' => null, 'migrationNamespaces' => [ // ... 'yii\queue\db\migrations', ], ], ],
能夠在console中 使用數據庫遷移工具遷移數據庫frontend
配置gii yii
'modules' => [ 'gii' => [ 'class' => 'yii\gii\Module', 'generators' => [ 'jobs' => yii\queue\gii\Generator::class, ], ] ],
能夠經過gii 建立job文件工具
取一個名字配置到 components 做爲一個組件
'queue' => [ 'class' => yii\queue\db\Queue::class, ], 'mutex' => yii\mutex\MysqlMutex::class,
使用:
經過 gii建立一個job類
namespace frontend\jobs; /** * Class TestJob. */ class TestJob extends \yii\base\BaseObject implements \yii\queue\JobInterface { public $prop1; public $prop2; /** * @inheritdoc */ public function execute($queue) { } }
push任務
Yii::$app->queue->push(new TestJob( [ 'prop1' => 1, 'prop2' => 2, ] ));