Sunday, February 12, 2017

Message Queuing - RabbitMQ






RabbitMQ


  • Accepts and forwards messages.
  • Components are
    • Producer (Forwards the messages)
    • Consumer (Receiver)
    • Queue (an infinite buffer of messages)
  • Confirms to AMQP (Advanced Message Queuing Protocol)


Steps of the Producer (Pyhthon code)
1. Establish the connenction

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

2. Create a Queue

channel.queue_declare(queue='hello')

3. Send the message to the queue (Using an exchange method)

channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print(" [x] Sent 'Hello World!'")


4. Close the connection
connection.close()

Steps of the Consumer 

1. Declare the Queue

channel.queue_declare(queue='hello')

2. Define the callback methodw which prints the message to be received

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


3. Define the Producer queue from which the messages should be received

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

4. Never ending loop which consumes messages continuously
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

No comments:

Post a Comment