How to configure multiple exchanges in laravel-queue-rabbitmq?

Viewed 1362

I have found famous library for using RabbitMQ in Laravel.

In configuration config/queue.php I can specify only one exchange and queue name. Does it support multiple exchanges?

2 Answers

For future people who will search the answer for this question. There is one(bad) way to add multiple exchanges. You have to duplicate rabbitmq connection with new exchange and when you will want to publish the message to new exchange, you will just change connection.

This is a very ugly way, but i didn't another one. Laravel queue doesn't provide a method to change the exchange, but there is the method onConnection what you help you.

Here is a simple example

        'conn_one' => [

        'driver' => 'rabbitmq',
        'queue' => env('RABBITMQ_QUEUE', 'default'),
        'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,

        'hosts' => [
            [
                'host' => env('RABBITMQ_HOST', '127.0.0.1'),
                'port' => env('RABBITMQ_PORT', 5672),
                'user' => env('RABBITMQ_USER', 'guest'),
                'password' => env('RABBITMQ_PASSWORD', 'guest'),
                'vhost' => env('RABBITMQ_VHOST', '/'),
            ],
        ],

        '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' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
                'exchange' => 'exchange_two',
                'exchange_type' => 'fanout',
            ],
        ],

     ],

    'conn_two' => [
        'driver' => 'rabbitmq',
        'queue' => env('RABBITMQ_QUEUE', 'default'),
        'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,

        'hosts' => [
            [
                'host' => env('RABBITMQ_HOST', '127.0.0.1'),
                'port' => env('RABBITMQ_PORT', 5672),
                'user' => env('RABBITMQ_USER', 'guest'),
                'password' => env('RABBITMQ_PASSWORD', 'guest'),
                'vhost' => env('RABBITMQ_VHOST', '/'),
            ],
        ],

        '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' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
                'exchange' => 'exchange_two',
                'exchange_type' => 'fanout',
            ],
        ],
    ],

Use

ExampleJob::dispach($data)->onConnection('conn_one');
ExampleJob::dispach($data)->onConnection('conn_two');

Or another ugly way could be to set config like this

Config::set('queue.connections.rabbitmq.options.queue.exchange', 'exchange_two');

before dispatching the job to the new exchange. In this way you dont have to create a duplicate connection just setting new exchange name before dispatching the job to the new exchange.

Related