My motivation was that I wanted to "peep" at the contents of the queue in order to display the current queue status on the service.
Obtained via WebAPI to check the queue contents of RabbitMQ, an AMQP broker. That's all you can get with pika.basic_consume () or pika.consume (), but use the Management API to avoid affecting other Consumers.
If you look at RabbitMQ's HTTP API documentation, you will see that the get item
Please note that the get path in the HTTP API is intended for diagnostics etc - it does not implement reliable delivery and so should be treated as a sysadmin's tool rather than a general API for messaging.
It may be a little subtle because it says
The operation check was done below.
The RabbitMQ Management Plugin must be enabled.
sudo rabbitmq-plugins enable rabbitmq_management
Send a message to the queue for confirmation.
publish.py
#!/usr/bin/env python
import pika
conn = pika.BlockingConnection(pika.ConnectionParameters())
ch = conn.channel()
ch.queue_declare(queue="hello")
ch.basic_publish(exchange="", routing_key="hello", body="Message 1")
ch.basic_publish(exchange="", routing_key="hello", body="Message 2")
conn.close()
Just get it using httplib.
view_messages.py
from base64 import b64encode
import httplib, json
API_HOST = "localhost" #Connection destination host name
API_PORT = 15672 #Connection port
def view_messages(vhost, queue, count=1):
#Assembly of URI etc.
uri = "/queues/%(vhost)s/%(queue)s/get" % {"vhost": vhost, "queue": queue}
auth = "guest:guest"
headers = {
"Authorization" : "Basic %s" % b64encode(auth),
"Content-Type" : "application/json",
}
opt = {"count": count, "requeue": "true", "payload_file": None, "encoding": "auto"}
#Connect to RabbitMQ Management to get messages
conn = httplib.HTTPConnection(API_HOST, API_PORT)
body = json.dumps(opt)
conn.request("POST", "/api%s" % uri, body, headers)
response = conn.getresponse()
return json.loads(response.read())
if __name__ == "__main__":
msgs = view_messages("%2F", "hello", count=100)
for msg in msgs:
print msg["payload"]
I'll give it a try.
./publish.py
./view_messages.py
Message 1
Message 2
Recommended Posts