50

I need to have a python client that can discover queues on a restarted RabbitMQ server exchange, and then start up a clients to resume consuming messages from each queue. How can I discover queues from some RabbitMQ compatible python api/library?

9 Answers 9

53

There does not seem to be a direct AMQP-way to manage the server but there is a way you can do it from Python. I would recommend using a subprocess module combined with the rabbitmqctl command to check the status of the queues.

I am assuming that you are running this on Linux. From a command line, running:

rabbitmqctl list_queues

will result in:

Listing queues ...
pings   0
receptions      0
shoveled        0
test1   55199
...done.

(well, it did in my case due to my specific queues)

In your code, use this code to get output of rabbitmqctl:

import subprocess

proc = subprocess.Popen("/usr/sbin/rabbitmqctl list_queues", shell=True, stdout=subprocess.PIPE)
stdout_value = proc.communicate()[0]
print stdout_value

Then, just come up with your own code to parse stdout_value for your own use.

Sign up to request clarification or add additional context in comments.

2 Comments

For me, this runs a wrapper script which refuses to continue if I'm not root. I can run the underlying binary (/usr/lib/rabbitmq/bin/rabbitmqctl) directly, though, if I make sure that my ~/.erlang.cookie file matches RabbitMQ's.
Running rabbitmqctl list_queues results in Error: could not recognise command
32

You can add plugin rabbitmq_management

sudo /usr/lib/rabbitmq/bin/rabbitmq-plugins enable rabbitmq_management
sudo service rabbitmq-server restart

Then use rest-api

import requests

def rest_queue_list(user='guest', password='guest', host='localhost', port=15672, virtual_host=None):
    url = 'http://%s:%s/api/queues/%s' % (host, port, virtual_host or '')
    response = requests.get(url, auth=(user, password))
    queues = [q['name'] for q in response.json()]
    return queues

I'm using requests library in this example, but it is not significantly.

Also I found library that do it for us - pyrabbit

from pyrabbit.api import Client
cl = Client('localhost:15672', 'guest', 'guest')
queues = [q['name'] for q in cl.get_queues()]

2 Comments

How do we get if the consumer is onine/ running ?
I suspect this will only show queues that the node has seen. There might be other queues on other nodes.
30

As far as I know, there isn't any way of doing this. That's nothing to do with Python, but because AMQP doesn't define any method of queue discovery.

In any case, in AMQP it's clients (consumers) that declare queues: publishers publish messages to an exchange with a routing key, and consumers determine which queues those routing keys go to. So it does not make sense to talk about queues in the absence of consumers.

Comments

6

Since I am a RabbitMQ beginner, take this with a grain of salt, but there's an interesting Management Plugin, which exposes an HTTP interface to "From here you can manage exchanges, queues, bindings, virtual hosts, users and permissions. Hopefully the UI is fairly self-explanatory."

http://www.rabbitmq.com/blog/2010/09/07/management-plugin-preview-release/

Comments

3

I use https://github.com/bkjones/pyrabbit. It's talks directly to RabbitMQ's mgmt plugin's API interface, and is very handy for interrogating RabbitMQ.

Comments

3

I found this works for me, /els being my demo vhost name..

rabbitmqctl list_queues --vhost /els

Comments

2

Management features are due in a future version of AMQP. So for now you will have to wait till for a new version that will come with that functionality.

Comments

0

pyrabbit didn't work so well for me; However, the Management Plugin itself has its own command line script that you can download from your own admin GUI and use later on (for example, I downloaded mine from

http://localhost:15672/cli/

for local use)

Comments

0

I would use simply this: Just replace the user(default= guest), passwd(default= guest) and port with your values.

import requests
import json

def call_rabbitmq_api(host, port, user, passwd):
  url = 'http://%s:%s/api/queues' % (host, port)
  r = requests.get(url, auth=(user,passwd))
  return r

def get_queue_name(json_list):
  res = []
  for json in json_list:
    res.append(json["name"])
  return res

if __name__ == '__main__':
  host = 'rabbitmq_host'
  port = 55672
  user = 'guest'
  passwd = 'guest'
  res = call_rabbitmq_api(host, port, user, passwd)
  print ("--- dump json ---")
  print (json.dumps(res.json(), indent=4))
  print ("--- get queue name ---")
  q_name = get_queue_name(res.json())
  print (q_name)

Referred from here: https://gist.github.com/hiroakis/5088513#file-example_rabbitmq_api-py-L2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.