Complete the code to acknowledge a message after processing it successfully.
channel.basic_ack(delivery_tag=[1])The method.delivery_tag uniquely identifies the message to acknowledge in RabbitMQ.
Complete the code to reject a message and requeue it for later processing.
channel.basic_reject(delivery_tag=[1], requeue=[2])
To reject and requeue a message, use basic_reject with the delivery tag and requeue=true.
Fix the error in the code to properly handle a failed message by rejecting without requeue.
channel.basic_reject(delivery_tag=[1], requeue=[2])
Use method.delivery_tag for the delivery tag and set requeue=false to discard the message.
Fill both blanks to create a dead-letter exchange binding for failed messages.
channel.queue_declare(queue='failed', arguments=[1]) channel.queue_bind(queue='failed', exchange=[2], routing_key='failed')
The queue is declared with a dead-letter exchange named 'dlx'. The queue is then bound to the 'dlx' exchange.
Fill all three blanks to implement a consumer callback that retries a message once before rejecting it.
def callback(ch, method, properties, body): retries = int(properties.headers.get('[1]', 0)) if properties.headers else 0 if retries < [2]: headers = properties.headers or {} headers['[3]'] = retries + 1 properties.headers = headers ch.basic_publish(exchange='', routing_key=method.routing_key, body=body, properties=properties) ch.basic_ack(delivery_tag=method.delivery_tag) else: ch.basic_reject(delivery_tag=method.delivery_tag, requeue=False)
The header 'x-retries' tracks retry count. The message retries once (less than 1), then rejects.