'Is there a way for pika get list of all routing_keys for a particular RabbitMQ exchange?

I have some code that uses pika to bind routing_keys to a RabbitMQ exchange:

ROUTING_KEYS = ["a", "b", "c"]

parameters = pika.ConnectionParameter(host=whatever, port=whatever, ...)
with pika.BlockingConnection(parameters=parameters) as connection:
  channel = connection.channel()
  channel.exchange_declare("my_exchange")
  channel.queue_declare("my_queue")
  for key in ROUTING_KEYS:
    channel.queue_bind(queue="my_queue", exchange="my_exchange", routing_key=key)

This works. But if I later change ROUTING_KEYS to be ["b","c","d"] it will correctly add "d" but it will not remove "a".

Is there a way to get the currently registered routing_keys of "my_exchange" so that I can compute the difference and automatically remove the ones that are no longer part of ROUTING_KEYS?

Thanks for any advice.


edit

Alternatively, is there any way in the message_callback to work out what the matching binding key was from RabbitMQ?



Solution 1:[1]

But if I later change ROUTING_KEYS to be ["b","c","d"] it will correctly add "d" but it will not remove "a".

You would have to use the queue_unbind method to remove the a binding.

Is there a way to get the currently registered routing_keys of "my_exchange" so that I can compute the difference and automatically remove the ones that are no longer part of ROUTING_KEYS?

You could use the HTTP API to get the list of bindings for a queue:

/api/queues/vhost/name/bindings

NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luke Bakken