Skip to content
Snippets Groups Projects
ood_consumer.py 1.12 KiB
Newer Older
Mitchell Moore's avatar
Mitchell Moore committed
#!/usr/bin/env python
import pika # python client
Mitchell Moore's avatar
Mitchell Moore committed

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))  # connecting to a broker on the local machine
Mitchell Moore's avatar
Mitchell Moore committed
channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct')  # create exchange to pass messages
Mitchell Moore's avatar
Mitchell Moore committed

result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue  # creates a random name for the newly generated queue
Mitchell Moore's avatar
Mitchell Moore committed

channel.queue_bind(
        exchange='direct_logs', queue=queue_name, routing_key="ood")  # combine exchange, queue, and define routing name
Mitchell Moore's avatar
Mitchell Moore committed

print(' [*] Waiting for logs. To exit press CTRL+C')


def callback(ch, method, properties, body):
    message = "ood sent this"
    channel.basic_publish(
        exchange='direct_logs', routing_key="manager", body=message)
Mitchell Moore's avatar
Mitchell Moore committed
    print(" [x] %r:%r" % (method.routing_key, body))
    # Todo: Make message manager more functional
Mitchell Moore's avatar
Mitchell Moore committed


channel.basic_consume(
    queue=queue_name, on_message_callback=callback, auto_ack=True)  # ingest messages, and assume delivered via auto_ack
channel.start_consuming()  # initiate message ingenstion