Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

php - Message queue RabbitMq with Laravel

I have installed the plugin for laravel vyuldashev/laravel-queue-rabbitmq.

I get the messages published by another application, then I have set the parameters in config/queue.php

'rabbitmq' => [

            'driver' => 'rabbitmq',
            'queue' => env('RABBITMQ_QUEUE', 'test'),
            'connection' => PhpAmqpLibConnectionAMQPLazyConnection::class,

            'hosts' => [
                [
                    'host' => env('RABBITMQ_HOST', 'xxx.xxx.xxx.xxx'),
                    'port' => env('RABBITMQ_PORT', 5672),
                    'user' => env('RABBITMQ_USER', 'xxxxx'),
                    'password' => env('RABBITMQ_PASSWORD', 'xxxxx),
                    'vhost' => env('RABBITMQ_VHOST', 'xxxxx'),
                ],
            ],

            'options' => [
                'ssl_options' => [
                    'cafile' => env('RABBITMQ_SSL_CAFILE', null),
                    'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
                    'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
                    'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
                    'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
                ],
                'queue' => [
                    'job' => AppQueueJobRabbitMQJob::class

                ],
            ],

            /*
             * Set to "horizon" if you wish to use Laravel Horizon.
             */
            'worker' => env('RABBITMQ_WORKER', 'default'),

        ], 

After I have create AppQueueJobRabbitMQJob:

<?php


namespace AppQueueJob;

use VladimirYuldashevLaravelQueueRabbitMQQueueJobsRabbitMQJob as BaseJob;


class RabbitMQJob extends BaseJob
{
    /**
     * Fire the job.
     *
     * @return void
     */
    public function fire()
    {
        $payload = $this->getRawBody();
        $class =MyJobClass::class;
        $method = 'handle';

        return ($this->instance = $this->resolve($class))->{$method}($this, $payload, $body);
    }
}

MyJobClass

<?php

namespace AppJobs;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;

class MyJobClass implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;


    public $job;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle($body, $payload)
    {
        echo PHP_EOL . json_encode($payload);
    }
}

Ok, I show the queue and get the job. In my broken I show my connection but I don't understand how to capture and show with echo the message in queue.

Thanks for all the helpers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...