From 3ecee6c2000c85d0213e6fba3528d30bf6d2ca78 Mon Sep 17 00:00:00 2001 From: Marco Zeisler Date: Sat, 10 Apr 2021 16:50:16 +0200 Subject: [PATCH] use exchange instead of queue to allow multiple subscribers --- .../.run/createsuperuser.run.xml | 24 ------------ .../entitiy_ident/.run/makemigrations.run.xml | 24 ------------ components/entitiy_ident/.run/migrate.run.xml | 24 ------------ .../entitiy_ident/.run/runserver.run.xml | 24 ------------ .../exampleReceiver.py | 38 +++++++++---------- msg_broker_rabbit_mq_example/exampleSender.py | 10 +++-- 6 files changed, 24 insertions(+), 120 deletions(-) delete mode 100644 components/entitiy_ident/.run/createsuperuser.run.xml delete mode 100644 components/entitiy_ident/.run/makemigrations.run.xml delete mode 100644 components/entitiy_ident/.run/migrate.run.xml delete mode 100644 components/entitiy_ident/.run/runserver.run.xml diff --git a/components/entitiy_ident/.run/createsuperuser.run.xml b/components/entitiy_ident/.run/createsuperuser.run.xml deleted file mode 100644 index 5fc8ae4..0000000 --- a/components/entitiy_ident/.run/createsuperuser.run.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - \ No newline at end of file diff --git a/components/entitiy_ident/.run/makemigrations.run.xml b/components/entitiy_ident/.run/makemigrations.run.xml deleted file mode 100644 index b341723..0000000 --- a/components/entitiy_ident/.run/makemigrations.run.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - \ No newline at end of file diff --git a/components/entitiy_ident/.run/migrate.run.xml b/components/entitiy_ident/.run/migrate.run.xml deleted file mode 100644 index 268a119..0000000 --- a/components/entitiy_ident/.run/migrate.run.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - \ No newline at end of file diff --git a/components/entitiy_ident/.run/runserver.run.xml b/components/entitiy_ident/.run/runserver.run.xml deleted file mode 100644 index 3f7b118..0000000 --- a/components/entitiy_ident/.run/runserver.run.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - \ No newline at end of file diff --git a/msg_broker_rabbit_mq_example/exampleReceiver.py b/msg_broker_rabbit_mq_example/exampleReceiver.py index 52a1328..5f8e7ea 100644 --- a/msg_broker_rabbit_mq_example/exampleReceiver.py +++ b/msg_broker_rabbit_mq_example/exampleReceiver.py @@ -1,28 +1,26 @@ -import pika, sys, os +import pika -# see https://www.rabbitmq.com/tutorials/tutorial-one-python.html +# see https://www.rabbitmq.com/tutorials/tutorial-three-python.html -def main(): - connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) - channel = connection.channel() +connection = pika.BlockingConnection( + pika.ConnectionParameters(host='localhost')) +channel = connection.channel() - channel.queue_declare(queue='hello') +channel.exchange_declare(exchange='logs', exchange_type='fanout') - def callback(ch, method, properties, body): - print(" [x] Received %r" % body) +result = channel.queue_declare(queue='', exclusive=True) +queue_name = result.method.queue - channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True) +channel.queue_bind(exchange='logs', queue=queue_name) - print(' [*] Waiting for messages. To exit press CTRL+C') - channel.start_consuming() +print(' [*] Waiting for logs. To exit press CTRL+C') -if __name__ == '__main__': - try: - main() - except KeyboardInterrupt: - print('Interrupted') - try: - sys.exit(0) - except SystemExit: - os._exit(0) +def callback(ch, method, properties, body): + print(" [x] %r" % body) + + +channel.basic_consume( + queue=queue_name, on_message_callback=callback, auto_ack=True) + +channel.start_consuming() diff --git a/msg_broker_rabbit_mq_example/exampleSender.py b/msg_broker_rabbit_mq_example/exampleSender.py index fee4de4..cad6470 100644 --- a/msg_broker_rabbit_mq_example/exampleSender.py +++ b/msg_broker_rabbit_mq_example/exampleSender.py @@ -1,13 +1,15 @@ import pika +import sys -# see https://www.rabbitmq.com/tutorials/tutorial-one-python.html +# see https://www.rabbitmq.com/tutorials/tutorial-three-python.html connection = pika.BlockingConnection( pika.ConnectionParameters(host='localhost')) channel = connection.channel() -channel.queue_declare(queue='hello') +channel.exchange_declare(exchange='logs', exchange_type='fanout') -channel.basic_publish(exchange='', routing_key='hello', body=b'Hello World!') -print(" [x] Sent 'Hello World!'") +message = ' '.join(sys.argv[1:]) or b"info: Hello World!" +channel.basic_publish(exchange='logs', routing_key='', body=message) +print(" [x] Sent %r" % message) connection.close()