Complete the code to publish a message to the default exchange in RabbitMQ.
channel.basic_publish(exchange=[1], routing_key='task_queue', body='Hello World!')
The default exchange in RabbitMQ is identified by an empty string ''. Publishing to it routes messages directly to the queue named in routing_key.
Complete the code to declare a queue that will receive messages from the default exchange.
channel.queue_declare(queue=[1], durable=True)
Queues must have a name to receive messages. Here, 'my_queue' is the queue name that matches the routing key used when publishing.
Fix the error in the code to correctly bind a queue to the default exchange.
channel.queue_bind(queue='my_queue', exchange=[1], routing_key='my_queue')
The default exchange cannot be bound to queues because it is predefined and automatically routes messages by queue name. Using an empty string '' as exchange name is correct, but binding is unnecessary.
Fill both blanks to create a message consumer that listens to the queue bound to the default exchange.
channel.basic_consume(queue=[1], on_message_callback=[2], auto_ack=True)
basic_consume.The consumer listens to the queue named 'my_queue'. The callback function handle_message processes incoming messages.
Fill all three blanks to declare a queue, publish a message to the default exchange, and consume messages from the queue.
channel.queue_declare(queue=[1], durable=True) channel.basic_publish(exchange=[2], routing_key=[3], body='Test Message') channel.basic_consume(queue=[1], on_message_callback=process_message, auto_ack=True)
The queue is declared as 'task_queue'. The message is published to the default exchange (empty string) with routing key matching the queue name. The consumer listens to the same queue.