provide easy example for rabbitMQ
This commit is contained in:
parent
e2e74dee24
commit
0c5aa41fd0
20
components/msg_broker_rabbit_mq_example/README.md
Normal file
20
components/msg_broker_rabbit_mq_example/README.md
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# Python Pika RabbitMQ example
|
||||||
|
* Get rabbitmq docker container <br>
|
||||||
|
`docker pull rabbitmq`
|
||||||
|
|
||||||
|
* Install requirements <br>
|
||||||
|
`pip install requirements.txt`
|
||||||
|
|
||||||
|
* Run rabbitmq container detached and expose port 5672 <br>
|
||||||
|
`docker run docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 rabbitmq`
|
||||||
|
|
||||||
|
* Run <br>
|
||||||
|
`python exampleReceiver.py` <br>
|
||||||
|
`[*] Waiting for messages. To exit press CTRL+C`
|
||||||
|
|
||||||
|
* Run <br>
|
||||||
|
`python exampleSender.py` <br>
|
||||||
|
`[x] Sent 'Hello World!'`
|
||||||
|
|
||||||
|
* exampleReceiver shows <br>
|
||||||
|
`[x] Received b'Hello World!'`
|
||||||
27
components/msg_broker_rabbit_mq_example/exampleReceiver.py
Normal file
27
components/msg_broker_rabbit_mq_example/exampleReceiver.py
Normal file
@ -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)
|
||||||
11
components/msg_broker_rabbit_mq_example/exampleSender.py
Normal file
11
components/msg_broker_rabbit_mq_example/exampleSender.py
Normal file
@ -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()
|
||||||
1
components/msg_broker_rabbit_mq_example/requirements.txt
Normal file
1
components/msg_broker_rabbit_mq_example/requirements.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
pika # rabbit mq client
|
||||||
Loading…
x
Reference in New Issue
Block a user