赞
踩
<?php
use Baiy\ThinkAsync\Subscribe\ConfigFileEventGetter;
use Baiy\ThinkAsync\Subscribe\DemoSubscriber;
use crmeb\subscribes\DemoAsyncSubscriber;
use crmeb\subscribes\DemoAsyncSubscriber01;
return [
// 异步执行默认队列
'async_exec_method_queue' => 'async_exec_method',
// 异步执行自定义队列
'async_exec_method_custom_queue' => [
'async_exec_method_custom' => '自定义异步执行队列',
'async_exec_method_custom01' => '自定义异步执行队列01',
],
// 异步订阅默认队列名称
'subscribe_default_queue' => 'subscribe_default',
// 异步订阅事件获取类
'subscribe_event_get_class' => ConfigFileEventGetter::class,
// 异步订阅事件配置(可通过修改`subscribe_event_get_class`改变配置来源)
'subscribe_event_config' => [
[
'name' => 'demo',
'title' => '演示事件',
'queue' => 'async_subscribe_demo', // 事件处理队列 为空使用异步订阅默认队列
// 事件订阅者配置
'subscriber' => [
[DemoSubscriber::class, 'handle'],
]
],
[
'name' => 'zidingyishijian',
'title' => '自定义事件',
'queue' => 'async_subscribe_zidingyi',
// 事件订阅者配置
'subscriber' => [
[DemoAsyncSubscriber::class, 'handle'],
[DemoAsyncSubscriber01::class, 'handle'],
]
],
]
];
<?php
namespace app\api\controller\demo;
use Baiy\ThinkAsync\Facade\Async;
/**
* 公共类
* Class PublicController
* @package app\api\controller
*/
class PublicController
{
//异步代码执行/异步延迟执行/异步事件订阅
//http://meishu.local/api/demo/demoasync
public function demoasync(){
// 异步执行默认队列
//Async::exec(self::class, 'test', 'exec');
//异步执行代码使用自定义队列
//Async::execUseCustomQueue(self::class, 'test','async_exec_method_custom', 'exec');
//Async::execUseCustomQueue(self::class, 'test','async_exec_method_custom01', 'exec');
// 异步延迟执行默认队列 延迟10秒
//Async::delay(10, self::class, 'test', 'delay');
// 异步延迟执行默认队列 延迟10秒
//Async::delayUseCustomQueue(10, self::class, 'test','async_exec_method_custom', 'delay');
// 获取所有队列标示
//var_dump(Async::queue());
// 获取队列长度
//var_dump(Async::queueSize('async_exec_method_custom'));
// 获取队列名称
//var_dump(Async::queueName('async_exec_method_custom'));
//事件订阅
//事件触发
Async::trigger('zidingyishijian',33,'lisi');
echo 11;
}
public static function test()
{
$arr['name'] = mt_rand(111111,999999);
$res = Db::name('demo')->insert($arr);
}
}
<?php
namespace crmeb\subscribes;
use think\facade\Db;
class DemoAsyncSubscriber
{
public static function handle($id,$name){
$arr['name'] = $id.$name;
$res = Db::name('demo')->insert($arr);
}
}
<?php
namespace crmeb\subscribes;
use think\facade\Db;
class DemoAsyncSubscriber01
{
public static function handle($id,$name){
$arr['name'] = $name.$id;
$res = Db::name('demo')->insert($arr);
}
}
php think queue:work --queue async_subscribe_zidingyi
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。