Middle
What does the consumer do after receiving a message from RabbitMQ? What happens to the message after processing?
sobes.tech AI
Answer from AI
After receiving a message, a RabbitMQ consumer typically performs the following actions:
- Processes the message — executes business logic, such as saving data to a database, calling external services, etc.
- Acknowledges receipt of the message to the RabbitMQ server if processing was successful.
What happens to the message after processing:
- If the consumer sends an acknowledgment (ack), RabbitMQ removes the message from the queue, considering it processed.
- If the consumer does not acknowledge the message (for example, in case of an error), the message may be re-sent to another consumer or placed in a dead-letter queue, depending on the configuration.
Example in Python using pika:
channel.basic_consume(queue='task_queue', on_message_callback=callback, auto_ack=False)
def callback(ch, method, properties, body):
# process the message
process(body)
# acknowledge
ch.basic_ack(delivery_tag=method.delivery_tag)