RabbitMQ - change binding of exchange

Viewed 698

I am using RabbitMq 3.7.16 and I have an exchange that I want to bind to one of two possible queues.

My use case is having the exchange bound to first queue and switching it to second queue, and then switch the binding back.

My Current Implementation

The exchange is of type direct and routing key for both is "".

When switching binding I perform Bind(second-queue) and then Unbind(first-queue). When switching back I do the opposite.

The problem

I've few milliseconds where both queues are bound to the exchange and thus receiving the same messages. I want every message to get to exactly one of the possible queues.

What is the right way to do such thing with RabbitMQ?

1 Answers

My Solution

Have two exchanges, one bound to first-queue and the other to second-queue. I defined 'alternate-exchange' property to first-exchange that will forward any message that cannot be routed to second-exchange.

When I want the messages to arrive to second-queue I simply unbind first-queue from the first-exchange. This make messages unroutable in the exchange and thus forwarded to second-exchange. When I want the messages to arrive again to first-queue I bind it back up.

Related