PHP code example of linzc / think-queue-rabbitmq

1. Go to this page and download the library: Download linzc/think-queue-rabbitmq 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/ */

    

linzc / think-queue-rabbitmq example snippets


[
    'default' => 'rabbitmq' // 驱动类型,可选择 sync(默认):同步执行,database:数据库驱动,redis:Redis驱动,rabbitmq:RabbitMQ驱动 //或其他自定义的完整的类名
]

'connections' => [
    // ...

    'rabbitmq' => [
        'type'       => Linzc\ThinkQueueRabbitMQ\queue\connector\RabbitMQ::class,
        'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,
        'queue'      => 'default',
        'hosts' => [
            [
                'host'     => '127.0.0.1',
                'port'     => 5672,
                'user'     => 'guest',
                'password' => 'guest',
                'vhost'    => '/',
            ],
        ],
        'options' => [
            'ssl_options' => [
                'cafile'      => null,
                'local_cert'  => null,
                'local_key'   => null,
                'verify_peer' => null,
                'passphrase'  => null,
            ],
            'queue' => [
                'job' => Linzc\ThinkQueueRabbitMQ\queue\job\RabbitMQ::class,
            ],
        ],
    ],

    // ...    
],

'connections' => [
    // ...

    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                // ...

                'prioritize_delayed' => false,
                'queue_max_priority' => 10,
            ],
        ],
    ],

    // ...    
],

'connections' => [
    // ...

    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                // ...

                'exchange' => 'exchange-name',
                'exchange_type' => 'topic',
                'exchange_routing_key' => '',
            ],
        ],
    ],

    // ...    
],

'connections' => [
    // ...

    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                // ...

                'reroute_failed' => true,
                'failed_exchange' => 'failed-exchange',
                'failed_routing_key' => 'failed_routing_key.%s',
            ],
        ],
    ],

    // ...    
],

namespace app\job;

use think\queue\Job;

class Job1{
    
    public function fire(Job $job, $data){
    
            //....这里执行具体的任务 
            
             if ($job->attempts() > 3) {
                  //通过这个方法可以检查这个任务已经重试了几次了
             }
            
            
            //如果任务执行成功后 记得删除任务,不然这个任务会重复执行,直到达到最大重试次数后失败后,执行failed方法
            $job->delete();
            
            // 也可以重新发布这个任务
            $job->release($delay); //$delay为延迟时间
          
    }
    
    public function failed($data){
    
        // ...任务达到最大重试次数后,失败了
    }

}



namespace app\lib\job;

use think\queue\Job;

class Job2{
    
    public function task1(Job $job, $data){
    
          
    }
    
    public function task2(Job $job, $data){
    
          
    }
    
    public function failed($data){
    
          
    }

}