📖 Scenario: You are building a simple messaging system where a publisher sends messages to a RabbitMQ queue. To ensure messages are safely received by the broker, you will use Publisher Confirms. This helps you know if your messages were successfully handled.
🎯 Goal: Create a RabbitMQ publisher that sends messages with publisher confirms enabled. You will set up the connection, enable confirms, publish a message, and handle the confirmation callback to print success or failure.
📋 What You'll Learn
Create a connection and channel to RabbitMQ
Enable publisher confirms on the channel
Publish a message to a queue named task_queue
Add a confirmation callback to print Message confirmed on success
Print Message NOT confirmed on failure
💡 Why This Matters
🌍 Real World
Publisher confirms are used in real messaging systems to guarantee that messages are not lost and are safely received by the broker before proceeding.
💼 Career
Understanding publisher confirms is important for DevOps and backend engineers working with message queues to build reliable, fault-tolerant systems.
Progress0 / 4 steps
1
Setup RabbitMQ connection and channel
Create a connection to RabbitMQ on localhost and open a channel named channel.
RabbitMQ
Hint
Use pika.ConnectionParameters('localhost') to connect locally.
2
Enable publisher confirms on the channel
Enable publisher confirms on the channel by calling channel.confirm_delivery().
RabbitMQ
Hint
Call confirm_delivery() on the channel to enable confirms.
3
Publish a message with confirmation handling
Publish the message 'Hello RabbitMQ!' to the queue 'task_queue' using channel.basic_publish. Use a try-except block to catch pika.exceptions.UnroutableError and print 'Message NOT confirmed' if it occurs. Otherwise, print 'Message confirmed' after publishing.
RabbitMQ
Hint
Use basic_publish with exchange='' and routing_key='task_queue'. Wrap in try-except to catch UnroutableError.
4
Close connection and print final confirmation
Close the connection after publishing and confirmation. The program should print exactly Message confirmed if the message was accepted by RabbitMQ.
RabbitMQ
Hint
Call connection.close() to cleanly close the connection.
Practice
(1/5)
1. What does enabling publisher confirms in RabbitMQ do?
easy
A. It automatically retries sending messages on failure.
B. It encrypts messages before sending.
C. It tells you if RabbitMQ accepted your message.
D. It queues messages without sending them.
Solution
Step 1: Understand the purpose of publisher confirms
Publisher confirms notify the sender if the message was accepted by RabbitMQ.
Step 2: Compare with other options
Retrying, encrypting, or queuing without sending are unrelated to confirms.
Final Answer:
It tells you if RabbitMQ accepted your message. -> Option C
A. confirmSelect() must be called before basicPublish()
B. waitForConfirms() cannot be called after publishing
C. basicPublish() requires a callback parameter
D. confirmSelect() should be called after waitForConfirms()
Solution
Step 1: Check order of method calls
confirmSelect() must be called before publishing to enable confirms.
Step 2: Identify the error
In the snippet, confirmSelect() is called after basicPublish(), so confirms are not enabled when publishing.
Final Answer:
confirmSelect() must be called before basicPublish() -> Option A
Quick Check:
Enable confirms before publishing [OK]
Hint: Call confirmSelect() before publishing messages [OK]
Common Mistakes:
Calling confirmSelect() after publishing
Thinking waitForConfirms() disables confirms
Believing basicPublish() needs a callback for confirms
5. You want to ensure your application only proceeds after RabbitMQ confirms message acceptance. Which sequence of calls correctly implements this using publisher confirms?
hard
A. channel.confirmSelect(); channel.basicPublish(...); channel.waitForConfirms();
B. channel.basicPublish(...); channel.waitForConfirms(); channel.confirmSelect();
C. channel.waitForConfirms(); channel.confirmSelect(); channel.basicPublish(...);
D. channel.basicPublish(...); channel.confirmSelect(); channel.waitForConfirms();
Solution
Step 1: Enable confirms before publishing
Call confirmSelect() first to enable confirms on the channel.
Step 2: Publish the message and wait for confirmation
After basicPublish(...), call waitForConfirms() to block until RabbitMQ confirms acceptance.
Final Answer:
channel.confirmSelect(); channel.basicPublish(...); channel.waitForConfirms(); -> Option A
Quick Check:
Enable confirms, publish, then wait [OK]
Hint: Enable confirms, publish, then waitForConfirms() [OK]
Common Mistakes:
Calling confirmSelect() after publishing
Waiting for confirms before publishing
Not calling waitForConfirms() at all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.