ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 用composer安装queue插件线 ``` ~~~ "require": { "php": ">=7.1.0", "topthink/framework": "dev-master", "topthink/think-captcha": "^1.0", "topthink/think-installer": "^1.0.14", "topthink/think-queue": "1.1.6", "topthink/think-helper": "^1.0.7", "karsonzhang/fastadmin-addons": "~1.2.4", "overtrue/pinyin": "^3.0", "phpoffice/phpspreadsheet": "1.12", "overtrue/wechat": "4.2.11", "nelexa/zip": "^3.3", "ext-json": "*", "ext-curl": "*", "ext-pdo": "*", "ext-bcmath": "*", "txthinking/mailer": "^2.0", "jaeger/querylist": "^4.2" }, ~~~ ``` ## 配置 ![](https://img.kancloud.cn/b2/e0/b2e06c649b443ad41b499a5b0264c49b_608x451.png) ``` ~~~ <?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: yunwuxin <448901948@qq.com> // +---------------------------------------------------------------------- return [ // 'connector' => 'Sync' 'connector' => 'Redis', // Redis 驱动 'expire' => 60, // 任务的过期时间,默认为60秒; 若要禁用,则设置为 null 'default' => 'default', // 默认的队列名称 'host' => '127.0.0.1', // redis 主机ip 'port' => 6379, // redis 端口 'password' => '', // redis 密码 'select' => 1, // 使用哪一个 db,默认为 db0 'timeout' => 0, // redis连接的超时时间 'persistent' => false, // 是否是长连接 ]; ~~~ ``` ## 新建队列处理函数 ![](https://img.kancloud.cn/7a/03/7a0306a4889ee5bbbf81eaabc5f3763f_374x329.png) ``` ~~~ <?php namespace app\message\controller; use QL\QueryList; use think\Exception; use think\Queue; use think\Queue\Job; use think\Db; class DoJob { /** * fire方法是消息队列默认调用的方法 * @param Job $job 当前的任务对象 * @param $data 发布任务时自定义的数据 * @return int */ public function fire(Job $job, $data) { //这里$data定义格式为:$data = [ 'type'=>1, 'data_id' => 123,'ts' => time()] if (empty($data)) { return 0; } // 有些消息在到达消费者时,可能已经不再需要执行了 // $isJobStillNeedToBeDone = $this->checkDatabaseToSeeIfJobNeedToBeDone($data); // if(!$isJobStillNeedToBeDone){ // $job->delete(); // return 0; // } if (is_array($data) && isset($data['type'])) { $type = $data['type']; if ($type == 1) { $isJobDone = $this->qudemo2($data['url'],$data['rules']); //执行发送邮件业务 //$isJobDone = $this->sendEmail($data['data_id']); } else if ($type == 2) { //执行APP推送消息业务 $isJobDone = $this->sendAppMessage($data['data_id']); } else if ($type == 3) { //执行订单业务 $isJobDone = $this->orderService($data['data_id']); } else { return 0; } } else { return 0; } if ($isJobDone) { // 如果任务执行成功,删除任务 $job->delete(); } else { if ($job->attempts() > 3) { //通过这个方法可以检查这个任务已经重试了几次了 $job->delete(); // 也可以重新发布这个任务 //$job->release(2); //$delay为延迟时间,表示该任务延迟2秒后再执行 } } } } ~~~ ``` ## 需要队列的地方直接用 ![](https://img.kancloud.cn/c3/f7/c3f71269a55081abcc28882651a38ff6_977x215.png) ![](https://img.kancloud.cn/44/22/4422a21513d7f7f4ae57f984fc6c1bd0_906x269.png) ### 注意注意注意 , 一定注意类名的选择,没有时可以用空 开启php think queue:work --daemon --queue Test 要用队列名 开启 php think queue:listen 不要队列名一定不用队列名