bigchungusdata/rabbit/consumer.py
2021-04-24 18:43:28 +02:00

29 lines
922 B
Python

#!/usr/bin/env python3
from datetime import datetime
import pika
DATETIMES=[]
def callback(ch, method, properties, body):
DATETIMES.append(datetime.now())
try:
n = 10
credentials = pika.PlainCredentials("rabbit", "rabbit")
connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost", credentials=credentials, heartbeat=0, socket_timeout=5))
channel = connection.channel()
channel.exchange_declare(exchange="test",exchange_type='direct')
queue_declare_result = channel.queue_declare(queue='testqueue', exclusive=False)
queue_name = queue_declare_result.method.queue
channel.queue_bind(exchange="test",routing_key='test', queue=queue_name)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
channel.start_consuming()
except Exception:
connection.close()
print(DATETIMES)