Pika

RabbitMQ is a message queuing system. A python tool called Pika is able to communicate with RabbitMQ and can prove useful for retrieving messages from error queues.

PRE-REQUISITE

You will need to install Pika. One of the easiest ways to do with is via pip.

INSTALLATION

To install pika you will need a python environment installed with pip. With pip installed you can simply run the following command :-

pip install pika

USAGE

In this example we are going to provide the code to extract all messages from an ERROR_QUEUE and compress them inside a gzip container. To execute it copy the code below and save into a file called pull_messages.py.

python pull_messages.py

————-——--CODE—————-—--

import pika
import gzip

credentials = pika.PlainCredentials('guest', 'guest')
parameters = pika.ConnectionParameters('<HOSTNAME>',5672,'/',credentials)

connection = pika.BlockingConnection(parameters)
channel = connection.channel()


def callback(ch, method, properties, body):
  with gzip.open("ERROR_QUEUE.gz", "at") as f:
    f.write('%r\n' % properties.headers)
    f.write('%s\n' % body.decode())
    f.close()


channel.basic_consume(
queue='ERROR_QUEUE', on_message_callback=callback, auto_ack=True)

try:
  channel.start_consuming()
except KeyboardInterrupt:
  channel.stop_consuming()
connection.close()

————-—---CODE———————--

Once you see all of the required messages have been consumed from the RabbitMQ ERROR_QUEUE you can then press CTRL + C to exit the python script. A file called ERROR_QUEUE.gz will now exist in the current directory. You can simply extract the file or read it with a tool like zcat and less.

zcat ERROR_QUEUE.gz | less

SCRIPT EXPLANATION

  1. Import both gzip and pika.

  2. Build variables containing the credentials and host details.

  3. Create the connection and channel.

  4. Define the call (in this case we want the message and all of the properties of the message) and destination file.

  5. Create a variable containing the queue details.

  6. Consume from the queue only stopping when there is a keyboard interupt.

FURTHER READING

Pika official site

PIP official site


Previous
Previous

SQLcl

Next
Next

Load Testing Tools