RabbitMQ

Erlang installation on Slackware

Requires Erlang:

Slackbuild

UNIX server installation

Python client

Examples

http://www.rabbitmq.com/tutorials/tutorial-one-python.html

Producer

Toggle line numbers
   1 #!/usr/bin/env python
   2 import pika
   3 connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
   4 channel = connection.channel()
   5 channel.queue_declare(queue='hello')
   6 channel.basic_publish(exchange='',  routing_key='hello', body='Hello World!')
   7 print " [x] Sent 'Hello World!'"
   8 connection.close()

Consumer

Toggle line numbers
   1 #!/usr/bin/env python
   2 import pika
   3 connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
   4 channel = connection.channel()
   5 channel.queue_declare(queue='hello')
   6 print ' [*] Waiting for messages. To exit press CTRL+C'
   7 
   8 def callback(ch, method, properties, body):
   9     print " [x] Received %r" % (body,)
  10 
  11 channel.basic_consume(callback, queue='hello', no_ack=True)
  12 channel.start_consuming()

Topic

Publish/Subscribe.

produceTopic.py

Toggle line numbers
   1 #!/usr/bin/env python
   2 import pika
   3 import sys
   4 
   5 if __name__=='__main__':
   6     exchangeName='broadcast'
   7     message = sys.argv[1]
   8 
   9     connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
  10     channel = connection.channel()
  11     channel.exchange_declare(exchange=exchangeName, type='fanout')
  12 
  13     channel.basic_publish(exchange=exchangeName, routing_key='', body=message)
  14     print(" [x] Producer sent message %s to topic %s" % (message , exchangeName) )
  15     connection.close()

consumeTopic.py

Toggle line numbers
   1 #!/usr/bin/env python
   2 import pika
   3 import sys
   4 
   5 def consumeTopic(ch, method, properties, body):
   6     print(" [x] %s received message %s" % (sys.argv[1] ,  body  ) )
   7 
   8 if __name__=='__main__':
   9     topicName='broadcast'
  10     connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost'))
  11     channel = connection.channel()
  12     channel.exchange_declare(exchange=topicName, type='fanout')
  13     result = channel.queue_declare(exclusive=True)
  14     privateQueue = result.method.queue
  15     channel.queue_bind(exchange=topicName, queue=privateQueue)
  16 
  17     print(' [*] %s waiting for broadcasts. To exit press CTRL+C'%( sys.argv[1] ) )
  18     channel.basic_consume(consumeTopic, queue=privateQueue, no_ack=True)
  19     channel.start_consuming()

Queue

Load balancer. Workers.

RabbitMQ (last edited 2016-09-08 15:29:17 by localhost)