0
0
RabbitMQdevops~5 mins

Direct exchange in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
A direct exchange in RabbitMQ routes messages to queues based on an exact matching routing key. It helps deliver messages precisely to the intended queue without confusion.
When you want to send a message to a specific queue identified by a unique key.
When you have multiple queues and want to control exactly which queue receives which message.
When you need simple routing without complex pattern matching.
When building a task queue where each task type has its own queue.
When you want to ensure messages do not get broadcasted to unintended queues.
Config File - direct_exchange_setup.sh
direct_exchange_setup.sh
#!/bin/bash

# Declare a direct exchange named 'my_direct_exchange'
curl -i -u guest:guest -H "content-type:application/json" \
  -XPUT \
  -d '{"type":"direct"}' \
  http://localhost:15672/api/exchanges/%2f/my_direct_exchange

# Declare a queue named 'task_queue'
curl -i -u guest:guest -H "content-type:application/json" \
  -XPUT \
  -d '{}' \
  http://localhost:15672/api/queues/%2f/task_queue

# Bind the queue to the exchange with routing key 'task_key'
curl -i -u guest:guest -H "content-type:application/json" \
  -XPOST \
  -d '{"routing_key":"task_key"}' \
  http://localhost:15672/api/bindings/%2f/e/my_direct_exchange/q/task_queue

This script creates a direct exchange called my_direct_exchange. It then creates a queue named task_queue. Finally, it binds the queue to the exchange using the routing key task_key. Messages sent to the exchange with this key will go to task_queue.

Commands
This command creates a direct exchange named 'my_direct_exchange' where messages will be routed based on exact routing keys.
Terminal
rabbitmqadmin declare exchange name=my_direct_exchange type=direct
Expected OutputExpected
Successfully declared exchange 'my_direct_exchange'
name - Specifies the name of the exchange to create
type - Sets the exchange type to direct for exact routing
This command creates a durable queue named 'task_queue' that will receive messages routed by the exchange.
Terminal
rabbitmqadmin declare queue name=task_queue durable=true
Expected OutputExpected
Successfully declared queue 'task_queue'
name - Specifies the queue name
durable - Makes the queue survive server restarts
This command binds the 'task_queue' to the 'my_direct_exchange' exchange with the routing key 'task_key'. Messages with this key go to this queue.
Terminal
rabbitmqadmin declare binding source=my_direct_exchange destination=task_queue destination_type=queue routing_key=task_key
Expected OutputExpected
Successfully declared binding from 'my_direct_exchange' to 'task_queue' with routing key 'task_key'
source - The exchange name
destination - The queue name
destination_type - Specifies that the destination is a queue
routing_key - The exact key used for routing messages
This command sends a message with the routing key 'task_key' to the direct exchange. It will be delivered to 'task_queue'.
Terminal
rabbitmqadmin publish exchange=my_direct_exchange routing_key=task_key payload='Hello Direct Exchange!'
Expected OutputExpected
Message published
exchange - Specifies which exchange to send the message to
routing_key - The routing key to match the queue binding
payload - The message content
This command retrieves the message from 'task_queue' to verify it was routed correctly.
Terminal
rabbitmqadmin get queue=task_queue requeue=false
Expected OutputExpected
Message count: 0 Payload: Hello Direct Exchange!
queue - Specifies the queue to get messages from
requeue - Whether to put the message back after getting it; false means remove it
Key Concept

If you remember nothing else from this pattern, remember: a direct exchange routes messages only to queues whose binding key exactly matches the message's routing key.

Common Mistakes
Using a routing key in the message that does not exactly match any queue binding key.
The message will not be delivered to any queue and will be lost or dropped.
Always ensure the routing key in the message matches exactly one of the queue binding keys.
Binding a queue to the exchange without specifying a routing key.
The binding will not work as expected because direct exchanges require routing keys for matching.
Always specify a routing key when binding a queue to a direct exchange.
Publishing messages to the wrong exchange type or name.
Messages will not be routed correctly and may be lost.
Double-check the exchange name and type before publishing messages.
Summary
Create a direct exchange to route messages by exact routing keys.
Declare queues and bind them to the exchange with specific routing keys.
Publish messages with routing keys to deliver them to the correct queue.
Retrieve messages from the queue to verify correct routing.