PHP code example of lswl / laravel-rabbitmq

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

    

lswl / laravel-rabbitmq example snippets


// 任务类
use Lswl\Rabbitmq\Jobs\BaseRabbitmq;

class CustomJob extends BaseRabbitmq
{
    // 自行实现逻辑
    public function handle()
    {
        // 调用任务时传递的数据为 msg
        // $this->msg->key === 'value';
    }
}

// 调用任务类
CustomJob::dispatch([
    'key' => 'value',
    ...
]);
shell
# 方式1: 使用 laravel 自带命令, 该命令使用 `basic_get` 方式
php artisan queue:work rabbitmq

# 方式2(推荐): 使用扩展包命令, 该命令使用 `basic_consume` 方式, 比 `basic_get` 性能高约2倍
php artisan rabbitmq:consume rabbitmq