We have a RabbitMQ exchange that is exchanging messages between several components of our system.
Each component is both a publisher and subscriber of the exchange.
We need to find a way of ensuring that each application does not receive messages it sends into the exchange.
For example.
App 1 sends a refresh message. We want this to go to all subscribers of the exchange apart from App 1
I can see that you can specify routing attributes, but this seems to define the messages you WANT not the ones you DO NOT WANT.
Any ideas?
What you're asking for is not directly possible with RabbitMQ. It's a bit backward from how RMQ works, and how the system should be designed.
That is, you can't filter or be "selective" about which messages a given consumer receives. Ultimately, the "selective consumer" is an anti-pattern in RabbitMQ. Once a message is in a queue, a consumer listening to that queue may receive that message (depending on how many consumers are attached, etc).
The best way to handle this, in my experience, is to either change your RMQ topology and application design so that this won't be a problem (may not be possible) or to have your app know which message it sent, and ignore anything it sent.
I think the first option is better (ensure this isn't going to happen with good app / topology design), but the second option is often easier...
To do this, you can add a custom property to your message, like "publisher-id" or similar. Have your consumer code check to make sure the publisher-id of the message does not match it's own publisher-id. If it does match, ack the message without doing anymore. If it does not match, process the message normally.
You can assign individual routing keys for each component (e.g. app1, app2, app3, ...) and subscribe each component to all other routing keys excepts its own.
So when App 1 sends a message it will send it with routing key app1 but it is subscribed only to app2, app3, ....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With