[PYTHON] View the contents of the queue using the RabbitMQ Management Web API

Overview

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.

Caution

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

Operation check

The operation check was done below.

Preparation

Enable Management Plugin

The RabbitMQ Management Plugin must be enabled.

sudo rabbitmq-plugins enable rabbitmq_management

Store messages in the queue

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

Obtained with Web API

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"]

Try it

I'll give it a try.

./publish.py
./view_messages.py
Message 1
Message 2

reference

Recommended Posts

View the contents of the queue using the RabbitMQ Management Web API
Hit the Web API using requests Example: Flickr
I tried using the API of the salmon data project
View using the python module of Nifty Cloud mobile backend
Try using the PeeringDB 2.0 API
Image Optimize on the server side using TinyPNG's Web API
[Python] I tried collecting data using the API of wikipedia
The story of creating a database using the Google Analytics API
Simulation of the contents of the wallet
Development of WEB application using Django [Add data from management screen]
[python, ruby] fetch the contents of a web page with selenium-webdriver
Proxy measures when using WEB API
Benefits and examples of using RabbitMq
Understand the contents of sklearn's pipeline
See the contents of Kumantic Segumantion
I tried using the checkio API
Try to get the road surface condition using big data of road surface management
Development and deployment of REST API in Python using Falcon Web Framework
Hit a method of a class instance with the Python Bottle Web API
I checked the contents of docker volume
Try using the Wunderlist API in Python
Try using the web application framework Flask
Try using the Kraken API in Python
Tweet using the Twitter API in Python
Create an application using the Spotify API
Read all the contents of proc / [pid]
Play with puns using the COTOHA API
Record custom events using the Shotgun API
Awareness of using Aurora Severless Data API
I tried using the BigQuery Storage API
Let Python measure the average score of a page using the PageSpeed Insights API
Create an easy-to-read pdf of laws and government ordinances using the law api
[Python] Automatically totals the total number of articles posted by Qiita using the API