我有一个要求,我需要读取 DLQ 的消息并据此执行适当的操作。早些时候,下面的代码片段在 RabbitMQ/Erlang 更新之前工作正常。现在它给出了 Bad Request 错误。
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 和 requests。但问题仍然存在。
是因为最新版本的 RabbitMQ 停止接受这种 REST 调用吗? 在 Python 中一一读取所有消息以执行所需操作的替代方法是什么?
提前致谢,
最佳答案
我们在这里引入了一项重大更改: https://github.com/rabbitmq/rabbitmq-management/pull/199
body 原始是:
body_raw =
{"count":5000,"ackmode":"ack_requeue_false",
"encoding":"auto","truncate":50000}
有 4 个选择,而不是 bool 值:
ackmode=ack_requeue_false
ackmode=ack_requeue_true
ackmode=reject_requeue_false
ackmode=reject_requeue_true
请阅读here了解更多详情
关于python - 如何一一读取RabbitMQ队列消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56039694/