在使用 pika 鏈接 RabbitMQ 的過程當中,發現只能獲取 ready 狀態的消息數量,只好用 http 請求來實現目的。python
#encoding: utf-8 #author: walker #date: 2018-03-06 #summary: 獲取 RabbitMQ 中3種狀態消息的數量 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 = '192.168.0.xx', vhost = 'vhost_walker', queue = 'queue_walker', user = 'walker', passwd = 'walker') ready, unacked, total = mqTool.getMessageCount() print('ready: %d' % ready) print('unacked: %d' % unacked) print('total: %d' % total)
*** walker ***json