概述

 

队列概念:版本:laravel version:5.1

消息队列的理解

Laravel 队列服务为各种不同的后台队列提供了统一的 API。队列允许你推迟耗时任务(例如发送邮件)的执

行,从而大幅提高 web 请求速度。

laravel实现的消息队列

 

1、配置:

· 为了更直观的看到队列执行的过程及结果,我们采用数据库驱动。

打开 config/queue.php,更改默认队列驱动为数据库:

default’ => env(‘QUEUE_DRIVER’, ‘database’)

 

为了使用database 队列驱动,需要一张数据库表来存放任务,要生成创建该表的迁移,运行 Artisan 命令queue:table ,迁移被创建好了之后,使用migrate 命令运行迁移:

 

php artisan queue:table

php artisan migrate


执行结果:

D: ewkqc ewbijia>php artisan queue:table
Migration created successfully!

D: ewkqc ewbijia>php artisan migrate
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrated: 2016_6_17_204335_create_jobs_table

 


2. 编写任务类

<?php
namespace AppJobs;

use AppJobsJob;
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsBusSelfHandling;
use IlluminateContractsQueueShouldQueue;

class TestJob extends Job implements SelfHandling, ShouldQueue
{

    public function handle(){
        $str='';
        for($i=1; $i < 100; $i++){
            $str .= ' hello world!';
        }
        file_put_contents('D:/zhouz.txt', $str);
    }

}

 

3、推送任务到队列

默认的 Laravel 控制器位于app/Http/Controllers/Controller.php 并使用了DispatchesJobs trait。该 trait 提供了一些允许你方便推送任务到队列的方法,例如dispatch 方法:


<?php
namespace AppControllers;

use AppHttpControllersController;
use AppJobsTestJob;

class TestjobController extends Controller{

    public function index(){
        $job = (new TestJob())->delay(60);
        $this->dispatch($job);
        echo 'success';
    }
}

这里需加了一个60s后,执行队列中的任务。

 

 

4、运行队列监听器

 

开启任务监听器 Laravel 包含了一个 Artisan 命令用来运行推送到队列的新任务。你可以使用 queue:listen

命令运行监听器:

php artisan queue:listen

还可以指定监听器使用哪个队列连接:

php artisan queue:listen connection

注意一旦任务开始后,将会持续运行直到手动停止。你可以使用一个过程监视器如 Supervisor 来确保队列监听

器没有停止运行。 队列优先级 你可以传递逗号分隔的队列连接列表到 listen 任务来设置队列优先级:

php artisan queue:listen --queue=high,low

在本例中, high 队列上的任务总是在从 low 队列移动任务之前被处理。 指定任务超时参数 你还可以设置每个

任务允许运行的最大时间(以秒为单位):

php artisan queue:listen --timeout=60

指定队列睡眠时间 此外,可以指定轮询新任务之前的等待时间(以秒为单位):

php artisan queue:listen --sleep=5

需要注意的是队列只会在队列上没有任务时“睡眠”,如果存在多个有效任务,该队列会持续运行,从不睡眠。

 

 

5、结果:

现在是来检验成果的时候了。以我本地环境为例。首先运行程序,产生几个任务: 
在浏览器中打开 对应任务类的访问地址:

 

这个时候打开数据库表jobs,可看到如下内容: 具体字段含义可以自己去查。 available_at 是执行时间,和crontab时间概念一致,到点就执行。我是隔了60s,所以 available_at比 created_at多60s。

id | queue | payload  | attempts  | reserved | reserved_at  |  available_at | created_at 

66    default    {"job":"Illuminate\Queue\CallQueuedHandler@call","data":{"command":"O:16:"App\Jobs\TestJob":2:{s:5:"queue";N;s:5:"delay";i:120;}"}}    0    0        1466155928    1466155808


然后观察运行监听程序的dos,当看到以下样子时,表示程序已经执行完毕:(ps我执行了多次这个任务,所以会有多条)


D: ewkqc ewbijia>php artisan queue:listen
Processed: IlluminateQueueCallQueuedHandler@call

这个时候,你去对应的目录,就会看到:zhouz.txt 这个文件了。

内容:如下:

hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world!
基本过程就是这样,整理mark。欢迎留言。

 


本文链接二维码可以保存在本地:保存

| 浏览 (5621) | (0) | (0) | | 2016-05-12 21:52:20 |


相关文章

  1. Mac安装Swoole扩展phpize 时 Cannot find autoconf 解决方法
  2. mac下安装swoole扩展报错PHP Startup Unable to load dynamic library
  3. Laravel 数据库事务
  4. PHP贷款等额本息、等额本金计算月供还款计划公式
  5. Laravel 关闭过滤排除部分路由VerifyCsrfToken 验证
  6. Laravel Horizon监控Redis队列queue