Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

Use these RabbitMQ topic exchange configurations if you're using RabbitMqBundle for symfony application. If you're using more than one consumers in your configs, consider using multiple_consumers config option instead. I have an example post somewhere that you can find and have a look into.


When producing messages to a queue with $this->producer->publish(json_encode($message, 'xxxxx'); method, remember to replace xxxxx (routing_key) parameters with routing_key values that you defined in config files. Example: create_order, bmw, audi, mercedes. Also, in the image below, RK stands for "routing_key" and BK stands for "binding_key".


Topic exchange facts



Remember


After creating or updating queues/exchanges configs, you should always run $ bin/console rabbitmq:setup-fabric so that all queues/exchanges from your symfony config are synchronized with rabbitmq server otherwise published messages won't reach queues as they won't exist.


1 Producer & 1 Exchange & 1 Queue & N Worker & 1 Consumer


All order "create" messages would go to single queue.


# You can run many workers like commands below.
$ app/console rabbitmq:consumer -m 100 order_create
$ app/console rabbitmq:consumer -m 100 order_create

old_sound_rabbit_mq:
connections:
default:
host: 127.0.0.1
port: 5672
user: guest
password: guest
vhost: /
lazy: true
producers:
order_create:
connection: default
exchange_options: { name: 'order_create_ex', type: topic }
consumers:
order_create:
connection: default
exchange_options: { name: 'order_create_ex', type: topic }
queue_options: { name: 'order_create_qu' }
callback: application_frontend.consumer.order_create



1 Producer & 1 Exchange & 2 Queue & N Worker & 2 Consumer


In this example, "bmw", "audi" and "mercedes" messages will be coming from a single producer but "bmw" orders get treated differently compared to "audi" and "mercedes" orders which get treated in same way.


# You can run many workers like commands below.
$ app/console rabbitmq:consumer -m 100 order_create_bmw
$ app/console rabbitmq:consumer -m 100 order_create_bmw

$ app/console rabbitmq:consumer -m 100 order_create_audi_mercedes
$ app/console rabbitmq:consumer -m 100 order_create_audi_mercedes

old_sound_rabbit_mq:
connections:
default:
host: 127.0.0.1
port: 5672
user: guest
password: guest
vhost: /
lazy: true
producers:
order_create:
connection: default
exchange_options: { name: order_create_ex, type: topic }
consumers:
order_create_bmw:
connection: default
exchange_options: { name: order_create_ex, type: topic }
queue_options:
name: order_create_bmw_qu
routing_keys:
- 'bmw.#'
callback: application_frontend.consumer.order_create_bmw
order_create_audi_mercedes:
connection: default
exchange_options: { name: order_create_ex, type: topic }
queue_options:
name: order_create_audi_mercedes_qu
routing_keys:
- 'audi.*'
- '#.mercedes.*'
callback: application_frontend.consumer.order_create_audi_mercedes




2 Producer & 1 Exchange & 2 Queue & N Worker & 2 Consumer


In this example, "bmw", "audi" and "mercedes" messages will be coming from two different producers. Producer X "bmw" messages get treated differently compared to the producer Y "audi" and "mercedes" messages which get treated in same way.


# You can run many workers like commands below.
$ app/console rabbitmq:consumer -m 100 order_create_bmw
$ app/console rabbitmq:consumer -m 100 order_create_bmw

$ app/console rabbitmq:consumer -m 100 order_create_audi_mercedes
$ app/console rabbitmq:consumer -m 100 order_create_audi_mercedes

old_sound_rabbit_mq:
connections:
default:
host: 127.0.0.1
port: 5672
user: guest
password: guest
vhost: /
lazy: true
producers:
order_create_bmw:
connection: default
exchange_options: { name: order_create_ex, type: topic }
queue_options:
name: order_create_bmw_qu
routing_keys:
- 'bmw.#'
order_create_audi_mercedes:
connection: default
exchange_options: { name: order_create_ex, type: topic }
queue_options:
name: order_create_audi_mercedes_qu
routing_keys:
- 'audi.*'
- '#.mercedes.*'
consumers:
order_create_bmw:
connection: default
exchange_options: { name: order_create_ex, type: topic }
queue_options:
name: order_create_bmw_qu
routing_keys:
- 'bmw.#'
callback: application_frontend.consumer.order_create_bmw
order_create_audi_mercedes:
connection: default
exchange_options: { name: order_create_ex, type: topic }
queue_options:
name: order_create_audi_mercedes_qu
routing_keys:
- 'audi.*'
- '#.mercedes.*'
callback: application_frontend.consumer.order_create_audi_mercedes