PHP code example of jcleng / think-queue-rabbitmq

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

    

jcleng / think-queue-rabbitmq example snippets


return [
    'connector' => '\jcleng\queue\connector\RabbitMQ',

    'dsn' => '',

    'host' => "127.0.0.1",
    'port' => 5672,

    'vhost' => '/',
    'login' => "guest",
    'password' => 'guest',

    'queue' => "queue_default",

    'options' => [

        'exchange' => [

            'name' => 'queue_exchange',

            /*
            * Determine if exchange should be created if it does not exist.
            */
            'declare' => true,

            /*
            * Read more about possible values at https://www.rabbitmq.com/tutorials/amqp-concepts.html
            */
            'type' => \Interop\Amqp\AmqpTopic::TYPE_DIRECT,
            'passive' => false,
            'durable' => true,
            'auto_delete' => false,
            'arguments' => null,
        ],

        'queue' => [

            /*
            * Determine if queue should be created if it does not exist.
            */
            'declare' => true,

            /*
            * Determine if queue should be binded to the exchange created.
            */
            'bind' => true,

            /*
            * Read more about possible values at https://www.rabbitmq.com/tutorials/amqp-concepts.html
            */
            'passive' => false,
            'durable' => true,
            'exclusive' => false,
            'auto_delete' => false,
            'arguments' => null,
        ],
    ],



];


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){


    }

}