I need to loop though all the messages in the queue for the callback and then close the call back. I need it to stop consuming once the queue is empty. So I'm writing the messages from one queue to another queue.
creds = pika.PlainCredentials(app.config['mq.user'], app.config['mq.pass'])
connection = pika.BlockingConnection(pika.ConnectionParameters(
host=app.config['mq.host'],
credentials=creds))
connection2 = pika.BlockingConnection(pika.ConnectionParameters(
host=app.config['mq.host'],
credentials=creds))
channel = connection.channel()
channel2 = connection2.channel()
Def requeue_callback(ch, method, properties, body):
try:
msg = json.loads(body)
ch2.basic_publish(exchange='',
routing_key=base_queue+'.request',
body = msg['orig_msg'])
finally:
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.basic_consume(requeue_callback,
queue=base_queue+'.error')
channel.start_consuming()
*Alternatively I could find the number of messages in a queue and then consume that specific number. In that case how would I re-queue a specific number.
In your callback function you can to declare queue with passive=True
, then use result to get message count.
For example:
>>> ch = rabbit.connection.channel()
>>> method = ch.queue_declare('sandbox', passive=True)
>>> method.method.message_count
23
>>>
Then check if this number is 0.