PHP code example of w7 / rangine-mq-cmq

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

    

w7 / rangine-mq-cmq example snippets


  'providers' => [
    // ...
    Freyo\LaravelQueueCMQ\LaravelQueueCMQServiceProvider::class,
  ]
  

  //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'))
  

'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',
        ],
    ];
    //...
];



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;
    }
}



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();
        }        
    }
}