66composer require easyswoole/queue
77```
88
9- ## 使用
10- 默认自带的队列驱动为Redis队列。
9+ ### 队列驱动
10+
11+ 任何队列驱动都必须实现``` EasySwoole\Queue\QueueDriverInterface ``` 这个接口定义。且实现的对象一定是必须可被克隆(可以看Queue中的Producer方法即知道为何)。
12+ 队列在被加载到对应topic的Producer和Consumer时,都会被分别执行一次init()方法。
13+
1114### 创建队列
1215``` php
1316
@@ -29,15 +32,17 @@ $queue = new Queue($driver);
2932```
3033$job = new Job();
3134$job->setJobData("this is my job data time time ".date('Ymd h:i:s'));
32- $queue->producer()->push($job);
35+ $queue->producer("topic" )->push($job);
3336```
3437### 普通消费
35- ```
36- $job = $queue->consumer()->pop();
38+ ``` php
39+
40+ $job = $queue->consumer("topic")->pop();
3741//或者是自定义进程中
38- $queue->consumer()->listen(function (Job $job){
42+ $queue->consumer("topicName" )->listen(function (Job $job){
3943 var_dump($job);
4044});
45+
4146```
4247
4348## CLI 单独使用
@@ -67,13 +72,42 @@ $sc->add(function (){
6772 while (1){
6873 Coroutine::sleep(3);
6974 $job = new Job();
70- $job->setJobData("job create at ".time());
71- $queue->producer()->push($job);
75+ $job->setJobData("job test create at ".time());
76+ try {
77+ $queue->producer("test")->push($job);
78+ }catch (\Throwable $throwable){
79+
80+ }
81+ }
82+ });
83+
84+ Coroutine::create(function ()use($queue){
85+ while (1){
86+ Coroutine::sleep(5);
87+ $job = new Job();
88+ $job->setJobData("job another create at ".time());
89+ try {
90+ $queue->producer("another")->push($job);
91+ }catch (\Throwable $throwable){
92+
93+ }
7294 }
7395 });
7496
75- $queue->consumer()->listen(function (Job $job){
76- var_dump($job->getJobData());
97+ Coroutine::create(function ()use($queue){
98+ $queue->consumer("test")->listen(function (Job $job){
99+ var_dump($job->getJobData() ." hande in test");
100+ },[],function (){
101+
102+ });
103+ });
104+
105+ Coroutine::create(function ()use($queue){
106+ $queue->consumer("another")->listen(function (Job $job){
107+ var_dump($job->getJobData() ." hande in another");
108+ },[],function (){
109+
110+ });
77111 });
78112
79113});
@@ -86,17 +120,17 @@ $sc->start();
86120$job = new Job();
87121$job->setJobData("this is my job data time time ".date('Ymd h:i:s'));
88122$job->setDelayTime(5);//设置延后时间
89- $queue->producer()->push($job);
123+ $queue->producer("topic" )->push($job);
90124```
91125## 可信任务
92126```
93127$job = new Job();
94128$job->setJobData("this is my job data time time ".date('Ymd h:i:s'));
95129$job->setRetryTimes(3);//任务如果没有确认,则会执行三次
96130$job->setWaitConfirmTime(5);//如果5秒内没确认任务,会重新回到队列。默认为3秒
97- $queue->producer()->push($job);//投递任务
131+ $queue->producer("topic" )->push($job);//投递任务
98132//确认一个任务
99- $queue->consumer()->confirm($job);
133+ $queue->consumer("topic" )->confirm($job);
100134```
101135
102136## 消费者控制
@@ -105,7 +139,7 @@ $queue->consumer()->confirm($job);
105139
106140``` php
107141/** @var \EasySwoole\Queue\Queue $queue */
108- $queue->consumer()->setOnBreak(function(\EasySwoole\Queue\Consumer $consumer) {
142+ $queue->consumer("topic" )->setOnBreak(function(\EasySwoole\Queue\Consumer $consumer) {
109143 // todo
110144 })->listen(function (\EasySwoole\Queue\Job $job){ });
111145```
@@ -114,5 +148,5 @@ $queue->consumer()->setOnBreak(function(\EasySwoole\Queue\Consumer $consumer) {
114148
115149``` php
116150/** @var \EasySwoole\Queue\Queue $queue */
117- $queue->consumer()->setBreakTime(0.1);
151+ $queue->consumer("topic" )->setBreakTime(0.1);
118152```
0 commit comments