这是从pika中提取队列的最简单方法

时间:2016-11-02 17:02:07

标签: python testing rabbitmq pika

好的人。 我对Python有点生疏,所以请放轻松我。 我有一个连接到的RabbitMQ服务器,我正在构建一个简单的界面进行健全性检查,我正在使用鼠兔。

我的代码看起来非常简单:

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

但是当我称这种方法时:

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

我的回复如下:

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'])>"])>

我想向用户展示的唯一事实就是这个队列

queue=amq.gen-8GVTfmr8noMB5slpXnFRHQ

从上述方法中提取此变量的最简单方法是什么? 谢谢。

p / s:log只是记录器的别名,而Config.USERNAMEConfig.PASSWORD只是用户&#39;和&#39; localhost&#39;

1 个答案:

答案 0 :(得分:1)

来自3rd rabbitmq tutorial,接收者代码:

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

引用上述链接(标题为临时队列):

  

...我们可以创建一个随机名称的队列,或者更好的是 - 让它   服务器为我们选择随机队列名称。我们可以做到这一点   将队列参数提供给queue_declare:

     

result = channel.queue_declare()
此时result.method.queue   包含随机队列名称。