Queue Message Queue:
Thinkphp official team developed a dedicated support queue service expansion pack, using the composer management, ease of use
Queue Message Queuing advantages:
-
Queue built Redis, Database, Topthink, Sync these four drivers, as used herein, Redis drive.
-
Queue message queue for large or complicated a little longer and return results require third party interface batch operations, may be used to send text messages, e-mail transmission, push the APP.
-
Message Queue message may be distributed or acquired, execution, deletion, retransmission failure handling, delayed execution, timeout control and other operations.
Queue Message Queue disadvantages:
- Once the need to process the data added to the task can not be deleted
The official address of the document:
https://packagist.org/packages/topthink/think-queue
Installation and Configuration:
-
Is installed, command Composer: composer require topthink / think-queue 2.0.x-dev (here, since the version 3.0 supports tp6 used herein, it is mounted tp5.1 Version 2.0.x-dev)
-
Configuration files are located in application / config / queue.php
-
As used herein, the type is Redis, if your current environment is not Redis, then install it yourself
-
Configuration files are as follows:
<?php return [ 'connector' => 'Redis', // 可选驱动类型:sync(默认)、Redis、database、topthink等其他自定义类型 'expire' => 60, // 任务的过期时间,默认为60秒; 若要禁用,则设置为 null 'default' => 'default', // 默认的队列名称 'host' => '127.0.0.1', // redis 主机ip 'port' => 6379, // redis 端口 'password' => '', // redis 连接密码 'select' => 0, // 使用哪一个 db,默认为 db0 'timeout' => 0, // redis连接的超时时间 'persistent' => false, // 是否是长连接 ];
-
The controller application \ test \ controller \ Index.php
<?php namespace app\test\controller; use think\Controller; use think\facade\Cache; use think\Queue; class Index extends Controller { public function index() { } /** * 一个使用了队列的 action */ public function actionWithHelloJob() { // 1.当前任务将由哪个类来负责处理。 // 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法 $jobHandlerClassName = 'app\test\controller\Hello'; // 2.当前任务归属的队列名称,如果为新队列,会自动创建 $jobQueueName = "helloJobQueue"; // 3.当前任务所需的业务数据 . 不能为 resource 类型,其他类型最终将转化为json形式的字符串 // ( jobData 为对象时,存储其public属性的键值对 ) $jobData = [ 'name' => 'supperl', 'age' => 22 ]; // 4.将该任务推送到消息队列,等待对应的消费者去执行 $isPushed = Queue::push($jobHandlerClassName, $jobData, $jobQueueName); // database 驱动时,返回值为 1|false ; redis 驱动时,返回值为 随机字符串|false if ($isPushed !== false) { echo date('Y-m-d H:i:s') . " a new Hello Job is Pushed to the MQ" . "<br>"; } else { echo 'Oops, something went wrong.'; } } }
-
The controller application \ test \ controller \ Hello.php
<?php namespace app\test\controller; use think\Controller; use think\queue\Job; class Hello extends Controller { /** * fire方法是消息队列默认调用的方法 * @param Job $job 当前的任务对象 * @param array|mixed $data 发布任务时自定义的数据 */ public function fire(Job $job, $data) { // 有些消息在到达消费者时,可能已经不再需要执行了 $isJobStillNeedToBeDone = $this->checkDatabaseToSeeIfJobNeedToBeDone($data); if (!$isJobStillNeedToBeDone) { $job->delete(); return; } $isJobDone = $this->doHelloJob($data); if ($isJobDone) { // 如果任务执行成功, 记得删除任务 $job->delete(); print("<info>Hello Job has been done and deleted" . "</info>\n"); } else { if ($job->attempts() > 3) { //通过这个方法可以检查这个任务已经重试了几次了 print("<warn>Hello Job has been retried more than 3 times!" . "</warn>\n"); $job->delete(); // 也可以重新发布这个任务 //print("<info>Hello Job will be availabe again after 2s."."</info>\n"); //$job->release(2); //$delay为延迟时间,表示该任务延迟2秒后再执行 } } } /** * 有些消息在到达消费者时,可能已经不再需要执行了 * @param array|mixed $data 发布任务时自定义的数据 * @return boolean 任务执行的结果 */ private function checkDatabaseToSeeIfJobNeedToBeDone($data) { return true; } /** * 根据消息中的数据进行实际的业务处理... */ private function doHelloJob($data) { print("<info>Hello Job Started. job Data is: " . var_export($data, true) . "</info> \n"); print("<info>Hello Job is Fired at " . date('Y-m-d H:i:s') . "</info> \n"); print("<info>Hello Job is Done!" . "</info> \n"); return true; } }
-
Perform create producers pushed to consumers
php index.php test/index/actionWithHelloJob
- display
2020-03-26 16:13:48 a new Hello Job is Pushed to the MQ<br>
- display
-
Consumer spending
php think queue:work --queue helloJobQueue
- display
<info>Hello Job Started. job Data is: array ( 'name' => 'supperl', 'age' => 22, )</info> <info>Hello Job is Fired at 2020-03-26 16:21:34</info> <info>Hello Job is Done!</info> <info>Hello Job has been done and deleted</info>
- display
-
Configuring supervisord process guard, continued ~ ~ ~