Routing messages in RabbitMQ topic exchange that do NOT match a pattern
Asked Answered
W

1

6

Two queues are bound to a topic exchange with the following routing keys:

Queue A, bound with routing key pattern match *.foo
Queue B, bound with routing key pattern match *.bar

I'd like to add a third queue to this exchange that receives messages that are neither foo messages nor bar messages. If I bind this queue with a # routing key, I naturally get all messages I need, but including foo's and bar's which I don't want.

Any way to route messages patching a pattern NOT *.foo AND NOT *.bar ?

Woodchopper answered 5/2, 2015 at 18:33 Comment(0)
C
7

If you want to catch all messages that doesn't match any bindings, that can be done with Alternate Exchange.

Add alternate exchange for existent one and collect all messages from that alternate exchanges:

standard workflow --> [main exchange (topic)]
                    |     --> via binding *.foo -->  [foo queue]
                    |     --> via binding *.bar -->  [bar queue]
                    v      
           [alternate exchange (let it be topic too)]
                    --> via binding * --> []

For more specific cases when you have N bindings but you want to catch all messages that doesn't match M bindings (where M < N) it is more problematic, but technically can be done via Dead Letter Exchange and then publish it to custom exchange where you have only M bindings, and then apply case with Alternate Exchange. But it even sounds rusty, not even think about performance degradation (applied only if you have really high messages flow).

Croatian answered 5/2, 2015 at 20:4 Comment(1)
I recommend not using an AE because adding a new binding (ie. an observer with '#') will prevent the AE/fallback from working. This is something that can happen in real life and make things go sideways really fast with unexpected interactions.Hellas

© 2022 - 2024 — McMap. All rights reserved.