标签:python
在使用 pika 连接 RabbitMQ 的过程中,发现只能获取 ready 状态的消息数量,只要用 http 请求来实现目的。
#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 ***
通过 http 方式获取 RabbitMQ 队列消息数量(python3)
标签:python
原文地址:http://blog.51cto.com/walkerqt/2083401