diff --git a/components/msg_broker_rabbit_mq_example/README.md b/components/msg_broker_rabbit_mq_example/README.md new file mode 100644 index 0000000..fec57d9 --- /dev/null +++ b/components/msg_broker_rabbit_mq_example/README.md @@ -0,0 +1,20 @@ +# Python Pika RabbitMQ example +* Get rabbitmq docker container
+ `docker pull rabbitmq` + +* Install requirements
+ `pip install requirements.txt` + +* Run rabbitmq container detached and expose port 5672
+ `docker run docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 rabbitmq` + +* Run
+ `python exampleReceiver.py`
+ `[*] Waiting for messages. To exit press CTRL+C` + +* Run
+ `python exampleSender.py`
+ `[x] Sent 'Hello World!'` + +* exampleReceiver shows
+ `[x] Received b'Hello World!'` \ No newline at end of file diff --git a/components/msg_broker_rabbit_mq_example/exampleReceiver.py b/components/msg_broker_rabbit_mq_example/exampleReceiver.py new file mode 100644 index 0000000..8ed2b7a --- /dev/null +++ b/components/msg_broker_rabbit_mq_example/exampleReceiver.py @@ -0,0 +1,27 @@ +import pika, sys, os + + +def main(): + connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) + channel = connection.channel() + + channel.queue_declare(queue='hello') + + def callback(ch, method, properties, body): + print(" [x] Received %r" % body) + + channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True) + + print(' [*] Waiting for messages. To exit press CTRL+C') + channel.start_consuming() + + +if __name__ == '__main__': + try: + main() + except KeyboardInterrupt: + print('Interrupted') + try: + sys.exit(0) + except SystemExit: + os._exit(0) diff --git a/components/msg_broker_rabbit_mq_example/exampleSender.py b/components/msg_broker_rabbit_mq_example/exampleSender.py new file mode 100644 index 0000000..4b6baa4 --- /dev/null +++ b/components/msg_broker_rabbit_mq_example/exampleSender.py @@ -0,0 +1,11 @@ +import pika + +connection = pika.BlockingConnection( + pika.ConnectionParameters(host='localhost')) +channel = connection.channel() + +channel.queue_declare(queue='hello') + +channel.basic_publish(exchange='', routing_key='hello', body=b'Hello World!') +print(" [x] Sent 'Hello World!'") +connection.close() diff --git a/components/msg_broker_rabbit_mq_example/requirements.txt b/components/msg_broker_rabbit_mq_example/requirements.txt new file mode 100644 index 0000000..65972db --- /dev/null +++ b/components/msg_broker_rabbit_mq_example/requirements.txt @@ -0,0 +1 @@ +pika # rabbit mq client \ No newline at end of file