refctor(rabbitmq): create fanout exchange_type
This commit is contained in:
parent
3730ca7828
commit
c7ce4f2fa8
16
rabbitmq/log.py
Normal file
16
rabbitmq/log.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
import pika
|
||||||
|
|
||||||
|
connection = pika.BlockingConnection(
|
||||||
|
pika.ConnectionParameters(host="localhost"),
|
||||||
|
)
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
channel.exchange_declare(exchange="logs", exchange_type="fanout")
|
||||||
|
|
||||||
|
message = " ".join(sys.argv[1:]) or "info: Hello World!"
|
||||||
|
channel.basic_publish(exchange="logs", routing_key="", body=message)
|
||||||
|
print(f" [x] Sent {message}")
|
||||||
|
|
||||||
|
connection.close()
|
42
rabbitmq/receive_log.py
Normal file
42
rabbitmq/receive_log.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import pika
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
connection = pika.BlockingConnection(
|
||||||
|
pika.ConnectionParameters(host="localhost"),
|
||||||
|
)
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
channel.exchange_declare(exchange="logs", exchange_type="fanout")
|
||||||
|
|
||||||
|
result = channel.queue_declare(queue="", exclusive=True)
|
||||||
|
queue_name = result.method.queue
|
||||||
|
|
||||||
|
channel.queue_bind(exchange="logs", queue=queue_name)
|
||||||
|
|
||||||
|
def callback(ch, method, properties, body):
|
||||||
|
print(f" [x] {body.decode()}")
|
||||||
|
|
||||||
|
print(" [*] Waiting for logs. To exit press CTRL+C")
|
||||||
|
channel.basic_consume(
|
||||||
|
queue=queue_name,
|
||||||
|
on_message_callback=callback,
|
||||||
|
auto_ack=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
channel.start_consuming()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("Interrupted")
|
||||||
|
try:
|
||||||
|
sys.exit(0)
|
||||||
|
except SystemExit:
|
||||||
|
os._exit(0)
|
Loading…
Reference in New Issue
Block a user