当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
8 Star 6 Fork 2

freyo / laravel-queue-cmq
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Tencent Cloud Message Queue Driver for Laravel Queue

FOSSA Status

Installation

composer require freyo/laravel-queue-cmq

Configure

Laravel 5.5+ uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

  1. config/app.php:
'providers' => [
  // ...
  Freyo\LaravelQueueCMQ\LaravelQueueCMQServiceProvider::class,
]
  1. .env:
QUEUE_DRIVER=cmq

CMQ_SECRET_KEY=
CMQ_SECRET_ID=

CMQ_QUEUE_HOST=https://cmq-queue-{region}.api.qcloud.com
CMQ_QUEUE=queue_name #default queue name
CMQ_QUEUE_POLLING_WAIT_SECONDS=0

CMQ_TOPIC_ENABLE=false # set to true to use topic
CMQ_TOPIC_FILTER=routing # or msgtag
CMQ_TOPIC_HOST=https://cmq-topic-{region}.api.qcloud.com
CMQ_TOPIC=topic_name

Tips

  • Region should be replaced with a specific region: gz (Guangzhou), sh (Shanghai), or bj (Beijing).

  • Domain for public network API request: cmq-queue-region.api.qcloud.com / cmq-topic-region.api.qcloud.com

  • Domain for private network API request: cmq-queue-region.api.tencentyun.com / cmq-topic-region.api.tencentyun.com

Usage

Once you completed the configuration you can use Laravel Queue API. If you used other queue drivers you do not need to change anything else. If you do not know how to use Queue API, please refer to the official Laravel documentation: http://laravel.com/docs/queues

Example

Dispatch Jobs

The default connection name is cmq

//use queue only
Job::dispatch()->onConnection('connection-name')->onQueue('queue-name');
// or dispatch((new Job())->onConnection('connection-name')->onQueue('queue-name'))

//use topic and tag filter
Job::dispatch()->onConnection('connection-name')->onQueue('tag1,tag2,tag3');
// or dispatch((new Job())->onConnection('connection-name')->onQueue('tag1,tag2,tag3'))

//use topic and routing filter
Job::dispatch()->onConnection('connection-name')->onQueue('routing-key');
// or dispatch((new Job())->onConnection('connection-name')->onQueue('routing-key'))

Multiple Queues

Configure config/queue.php

'connections' => [
    //...
    'new-connection-name' => [
        'driver' => 'cmq',
        'secret_key' => 'your-secret-key',
        'secret_id'  => 'your-secret-id',
        'queue' => 'your-queue-name',
        'options' => [
            'queue' => [
                'host'                 => 'https://cmq-queue-region.api.qcloud.com',
                'name'                 => 'your-queue-name',
                'polling_wait_seconds' => 0, // 0-30 seconds
                'retries'              => 3,
            ],
            'topic' => [
                'enable'  => false,
                'filter'  => 'routing', // routing or msgtag
                'host'    => 'https://cmq-topic-region.api.qcloud.com',
                'name'    => 'your-topic-name',
                'retries' => 3,
            ],
        ],
        'plain' => [
            'enable' => false,
            'job' => 'App\Jobs\CMQPlainJob@handle',
        ],
    ];
    //...
];

Process Jobs

php artisan queue:work {connection-name} --queue={queue-name}

Plain Mode

Configure .env

CMQ_PLAIN_ENABLE=true
CMQ_PLAIN_JOB=App\Jobs\CMQPlainJobHandler@handle

Create a job implements PlainPayload interface. The method getPayload must return a sting value.

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Freyo\LaravelQueueCMQ\Queue\Contracts\PlainPayload;

class CMQPlainJob implements ShouldQueue, PlainPayload
{
    use Dispatchable, InteractsWithQueue, Queueable;
    
    protected $payload;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($payload)
    {
        $this->payload = $payload;
    }
    
    /**
     * Get the plain payload of the job.
     *
     * @return string
     */
    public function getPayload()
    {
        return $this->payload;
    }
}

Create a plain job handler

<?php

namespace App\Jobs;

use Illuminate\Queue\Jobs\Job;

class CMQPlainJobHandler
{
    /**
     * Execute the job.
     * 
     * @param \Illuminate\Queue\Jobs\Job $job
     * @param string $payload
     * 
     * @return void
     */
    public function handle(Job $job, $payload)
    {
        // processing your payload...
        var_dump($payload);
        
        // release back to the queue manually when failed.
        // $job->release();
        
        // delete message when processed.
        if (! $job->isDeletedOrReleased()) {
            $job->delete();
        }        
    }
}

References

License

The MIT License (MIT). Please see License File for more information.

FOSSA Status

MIT License Copyright (c) 2017 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Tencent Cloud Message Queue driver for Laravel Queue 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/freyo/laravel-queue-cmq.git
git@gitee.com:freyo/laravel-queue-cmq.git
freyo
laravel-queue-cmq
laravel-queue-cmq
master

搜索帮助