RabbitMQ Tutorial


RABBITMQ is a message broker which works as a common channel for different entities. The main purpose of RABBITMQ is clustering and high availability. Message producer entities put message to the RABBITMQ and then the consumers get it according to work plan. If some of the producers or consumers fail the remainings still works. Advenced Messaging Queuing Protocol (AMQP) is the main standart that is implemented  by RABBITMQ.

RABBITMQ gives reliability to the application with acknowledgements and publisher confirms. Messages can be bind to the different entities according to the given logic. Several RABBITMQ servers can be clustered which makes high availability. RABBITMQ can federate several different applications and makes it easy. Here can be given more examples but theses are enough.

Installation ofRabbitMQ

Install RABBITMQ to the fedora

$ yum install rabbitmq-server.noarch

Install Python Library For RabbitMQ

In this tutorial we will use python language. But we need a library which supports AMQP. We choose to use pika so we install it with the following command

$ yum install python-pika.noarch

We need to firstly check the status of the rabbitmq server. Here we get that it is not started and we start it with default configuration

Start RabbitMQ Server Daemon

[[email protected] ismail]# systemctl start rabbitmq-server
[[email protected] ismail]# systemctl status rabbitmq-server
rabbitmq-server.service - RabbitMQ broker
Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; disabled)
Active: active (running) since Sat 2014-11-22 04:25:40 EET; 1s ago
Main PID: 9179 (beam.smp)

...

Create Producer

We will write some producer to create messages to ht RabbitMQ server. We put following code into p1.py 

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='Selam')

channel.basic_publish(exchange='',
routing_key='hello',
body='Selam Naber')
print " [x] Sent 'Selam Naber'"
connection.close()

Now run the producer and send the message to the queue.

$ python p1.py
[x] Sent 'Selam Naber'

Create Consumer

Here is the consumer which waits after running for a message. File name of the consumer is c1.py 

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

print ' [*] Waiting for messages. To exit press CTRL+C'

def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)

channel.basic_consume(callback,
queue='hello',
no_ack=True)

channel.start_consuming()

Start Consuming Producers Messages

If we run producer we get the message

$ python c1.py
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'Selam Naber'

 

LEARN MORE  List of FTP Commands For Linux and Windows with Examples

RabbitMQ Tutorial Infographic

RabbitMQ Tutorial Infographic
RabbitMQ Tutorial Infographic

 

Leave a Comment