Jobtest.php
1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace app\index\controller\job;
use think\Queue;
class Jobtest
{
//生产者测试
public function test()
{
// 1.当前任务将由哪个类来负责处理。
// 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法
$jobHandlerClassName = 'app\index\controller\job\Project';
// 2.当前任务归属的队列名称,如果为新队列,会自动创建
$jobQueueName = "checkItem";
// 3.当前任务所需的业务数据 . 不能为 resource 类型,其他类型最终将转化为json形式的字符串
$jobData = ['name' => 'test_01'] ;
// 4.将该任务推送到消息队列,等待对应的消费者去执行
// $isPushed = Queue::push( $jobHandlerClassName , $jobData , $jobQueueName );
$isPushed = Queue::later(30,$jobHandlerClassName,$jobData,$jobQueueName); //把任务分配到队列中,延迟10s后执行
// 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 'something went wrong.';
}
}
}