Anil Kumar
Anil Kumar

Reputation: 103

How to read RabbitMQ queue messages one by one

I have a requirement where I need to read the messages of DLQ and do appropriate action based on that. Earlier, below code snippet was working fine before the RabbitMQ/Erlang update. Now it gives Bad Request error.

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())
...

So, I tried to change this and used new libraries of Python like urllib.request, urllib.error, urllib.parse, and requests. But issue persists.

Is it because latest version of RabbitMQ stopped accepting this kind of REST call? What could be alternative to read all messages one by one in Python for performing required action?

thanks in advance,

Upvotes: 0

Views: 2941

Answers (1)

Gabriele Santomaggio
Gabriele Santomaggio

Reputation: 22682

We introduced a breaking change here: https://github.com/rabbitmq/rabbitmq-management/pull/199

The body raw is:

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

There are 4 choises, instead of a boolean:

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

please read here for more detail

Upvotes: 2

Related Questions