sql/app/controller/JobTest.php

38 lines
1.5 KiB
PHP
Raw Normal View History

2024-03-07 12:31:01 +08:00
<?php
/**
* 文件路径: \application\index\controller\JobTest.php
* 该控制器的业务代码中借助了thinkphp-queue 库,将一个消息推送到消息队列
*/
namespace app\controller;
use think\Exception;
//use think\Queue;
use think\facade\Queue;
class JobTest {
/**
* 一个使用了队列的 action
*/
public function actionWithHelloJob(){
// 1.当前任务将由哪个类来负责处理。
// 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法
$jobHandlerClassName = 'app\job\Hello';
// 2.当前任务归属的队列名称,如果为新队列,会自动创建
$jobQueueName = "helloJobQueue";
// 3.当前任务所需的业务数据 . 不能为 resource 类型其他类型最终将转化为json形式的字符串
// ( jobData 为对象时存储其public属性的键值对 )
2024-03-08 17:50:07 +08:00
$jobData = ['ts' => time(), 'bizId' => uniqid(), 'a' => 1,'title'=>"周雪莲子",'body'=>'女神节快乐'];
2024-03-07 12:31:01 +08:00
// 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.';
}
}
}