provide easy example for rabbitMQ

This commit is contained in:
Marco Zeisler 2021-04-10 15:59:28 +02:00
parent e2e74dee24
commit 0c5aa41fd0
4 changed files with 59 additions and 0 deletions

View 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!'`

View 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)

View 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()

View File

@ -0,0 +1 @@
pika # rabbit mq client