Search code examples
javaspring-bootspring-cloud-streamspring-rabbit

Create and bind queues dynamically to a single Exchange


I have an exchange (exchange1) and it can be routed to n different queues based on routing-key-expression.

  1. All Images messages should go to queue1
  2. All Document messages should go to queue2
  3. All video messages should go to default queue3

and in future, the queue number can be increased like (all video and mp4 extensions should go to queue4)

How we can create and bind queues dynamically to one particular exchange and should use only one stream Listener?


Solution

  • It can't be done directly with Spring Cloud Stream via properties.

    You would have to declare Exchange, Queue and Binding @Beans with the required routing keys and then configure the consumer binding to not declare the queue and binding) and then set s.c.s.consumer.multiplex=true and s.c.s.destination=queue1,queue2,queue3.

    See Using Existing Queues/Exhanges for how to disable binder provisioning.