如何一一阅读RabbitMQ队列消息

时间:2019-05-08 11:25:53

标签: python rabbitmq

我有一个要求,我需要阅读DLQ消息并据此采取适当的措施。之前,在RabbitMQ / Erlang更新之前,下面的代码片段可以正常工作。现在出现错误请求错误。

import urllib2
import json
import optparse

class http_worker:

    def authentication(self, url, user, pw):
        password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() 
        password_manager.add_password(None, url, user, pw)

        self.auth = urllib2.HTTPBasicAuthHandler(password_manager) 
        opener = urllib2.build_opener(self.auth) 
        urllib2.install_opener(opener)

    def call_url(self, url, body_raw):
        body = json.dumps(body_raw)
        #
        # urllib2 post since there is body 
        #
        req = urllib2.Request(url, body, {'Content-Type': 'application/json'})
        return urllib2.urlopen(req)

user = "guest"
pwd = "guest"
rabbit_host = "http://localhost:15672"
host_suffix = "/api/queues/%%2F/%s/get" %(rabbit_queue_name)

url = rabbit_host + host_suffix
body_raw = {"count":5000,"requeue":True,"encoding":"auto","truncate":50000}

worker = http_worker()
worker.authentication(url, user, pwd)
res = worker.call_url(url, body_raw)
result = json.loads(res.read())
...

因此,我试图对此进行更改,并使用了新的Python库,例如urllib.request,urllib.error,urllib.parse和请求。但是问题仍然存在。

是因为最新版本的RabbitMQ停止接受这种REST调用吗? 在Python中一一读取所有消息以执行所需操作的另一种选择是什么?

预先感谢

1 个答案:

答案 0 :(得分:2)

我们在这里介绍了一项重大更改: https://github.com/rabbitmq/rabbitmq-management/pull/199

未加工的身体是:

body_raw = 
{"count":5000,"ackmode":"ack_requeue_false",
"encoding":"auto","truncate":50000}

有4种选择,而不是布尔型:

ackmode=ack_requeue_false
ackmode=ack_requeue_true
ackmode=reject_requeue_false
ackmode=reject_requeue_true 

请阅读here了解更多详情