4

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?

jbx
  • 21,365
  • 18
  • 90
  • 144
POV
  • 11,293
  • 34
  • 107
  • 201
  • I mean register some exchange in server side using Laravel, cause now config provides only one exchange name – POV Dec 19 '18 at 21:27
  • In RabbitMQ you always post messages on an exchange and consume messages from a queue. Still not clear what you mean. I think Laravel refers to exchanges (which are solely a RabbitMQ concept) as Queues in general (SQS, Redis etc.). Did you have a look at this: https://laravel.com/docs/5.7/queues#connections-vs-queues ? – jbx Dec 19 '18 at 21:29
  • Okay, what does mean exchange configuration in setting of this library? – POV Dec 19 '18 at 21:31
  • I know I ask why in configuration of this drive there is only one exchange? – POV Dec 19 '18 at 21:34
  • `'options' => [ 'exchange' => [ 'name' => env('RABBITMQ_EXCHANGE_NAME'),....` – POV Dec 19 '18 at 21:35
  • Sure, that is question, why this driver supports only one exchange and queue? – POV Dec 19 '18 at 21:36
  • Maybe you can recommend another more flexible library for Laravel – POV Dec 19 '18 at 22:08

2 Answers2

8

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');
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

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.