PHP code example of limingxinleo / x-swoole-queue

1. Go to this page and download the library: Download limingxinleo/x-swoole-queue library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

limingxinleo / x-swoole-queue example snippets


use Xin\Swoole\Queue\Job;

$config =  = $config['redisAuth'];
$db = $config['redisDb'];
$port = $config['redisPort'];

$queue = new Job();
$queue->setRedisConfig($host, $auth, $db, $port)
    ->setPidPath(TESTS_PATH . 'queue2.pid')
    ->run();
~~~
消息类
~~~php

namespace Tests\Test\App;

use Xin\Support\File;
use Xin\Swoole\Queue\JobInterface;

class TestJob implements JobInterface
{
    public $data;

    public $file = TESTS_PATH . '/test.cache';

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function handle()
    {
        File::getInstance()->put($this->file, $this->data);
    }

use Tests\Test\App\TestJob;
use Xin\Redis;

$redis = Redis::getInstance(); 
$job = new TestJob('upgrade by test job!');
$redis->lPush('swoole:queue:queue', serialize($job));
~~~

## 高级使用办法
实现我们自己的消息队列类
~~~php

namespace Tests\Test\App;

use Xin\Swoole\Queue\Job;

class Queue extends Job
{
    public function __construct()
    {
        $config = 



use Tests\Test\App\Queue;

$config =  $config['redisAuth'];
$db = $config['redisDb'];
$port = $config['redisPort'];

$queue = new Queue();
$queue->run();
~~~

载入消费数据
~~~php

use Tests\Test\App\Queue;
use Xin\Swoole\Queue\JobInterface;

class TestJob implements JobInterface
{
    public $msg;

    public function __construct($msg)
    {
        $this->msg = $msg;
    }

    public function handle()
    {
        echo $this->msg;
    }