Python 查看RabbitMQ队列长度

在使用 pika 连接 RabbitMQ 的过程中,发现只能获取 ready 状态的消息数量,只好用 http 请求来实现目的。


import os, sys, time
import requests
import json


class RabbitMQTool(object):
    def __init__(self, host, vhost, queue, user, passwd):
        self.host = host
        self.vhost = vhost
        self.queue = queue
        self.user = user
        self.passwd = passwd

    # 返回3种消息数量:ready, unacked, total
    def getMessageCount(self):
        url = 'http://%s:15672/api/queues/%s/%s' % (self.host, self.vhost, self.queue)
        r = requests.get(url, auth=(self.user, self.passwd))
        print(r)
        if r.status_code != 200:
            return -1

        dic = json.loads(r.text)

        return dic['messages_ready'], dic['messages_unacknowledged'], dic['messages']


if __name__ == '__main__':
    mqTool = RabbitMQTool(host='127.0.0.1',
                          vhost='bdp-datacenter-spider',
                          queue='general_item_queue_new',
                          user='bdp-spider',
                          passwd='')

    ready, unacked, total = mqTool.getMessageCount()

    print('ready: %d' % ready)
    print('unacked: %d' % unacked)
    print('total: %d' % total)

你可能感兴趣的:(Python 查看RabbitMQ队列长度)