mutantkeyboard mutantkeyboard - 1 month ago 10
Python Question

Which is the easiest way to extract queue from pika

Ok people.
I'm bit rusty with Python so please go easy on me.
I have a RabbitMQ server that I connect to, and I'm building a simple interface for sanity check, and I'm using pika.

My code looks very simple:

class RabbitMQConnector(object):
""" Class that instantiates the connection, and the test interface for
the RabbitMQ services.
"""

def __init__(self):
self._credentials = pika.PlainCredentials(Config.USERNAME, Config.PASSWORD)
log.info("Credentials are being set")
self._parameters = pika.ConnectionParameters(Config.HOST, credentials=self._credentials)
log.info("Connection parameters are being set")
self._connection = pika.BlockingConnection(self._parameters)
self._channel = self._connection.channel()
# We pass an empty string into the queue so we can get the random queue name
self._replyQueue = self._channel.queue_declare(queue='')
log.info("Reply queue name has been initialized %s" % self._replyQueue)
log.info("Connection initialized")
Config.time.sleep(5)
log.info("[x] System is fully initialized, and ready to accept RabbitMQ connections")

def connect(self):
pass

def disconnect(self):
pass


But when I call this method:

log.info("Reply queue name has been initialized %s" % self._replyQueue)


My reply gets like:

Reply queue name has been initialized <METHOD(['channel_number=1', 'frame_type=1', "method=<Queue.DeclareOk(['consumer_count=0', 'message_count=0', 'queue=amq.gen-8GVTfmr8noMB5slpXnFRHQ'])>"])>


And the only thing I want to display to user is actually this queue

queue=amq.gen-8GVTfmr8noMB5slpXnFRHQ


What would be the easiest way to extract this variable from the above method?
Thank you.

p/s:
log
is just an alias for logger, whilst
Config.USERNAME
and
Config.PASSWORD
are just 'user' and 'localhost'

Answer

From the 3rd rabbitmq tutorial, code for receiver:

result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

Quote from the aforementioned link (under title Temporary queues):

...we could create a queue with a random name, or, even better - let the server choose a random queue name for us. We can do this by not supplying the queue parameter to queue_declare:

result = channel.queue_declare()
At this point result.method.queue contains a random queue name.

Comments