RabbitMQ - Header Exchange

RabbitMQ has a few different types of exchanges.

  • Direct Exchange
  • Fanout Exchange
  • Header Exchange
  • Topic Exchange

A header exchange uses headers instead of routing keys to determine where a message should be routed. For example, let's say there is a header exchange named header.exchange and a queue named header.queue. In this scenario, header.exchange could be bound to header.queue with the following arguments.

  • foo = bar
  • x-match = any or x-match = all

 

AVOID TROUBLE

Only simple key value pairs can be used. Complex keys, such as a nested or multidimensional key will fail to match.

 

Now let's say a message is published to header.exchange with header foo = bar.

In this scenario, since header.exchange is bound to header.queue with argument foo = bar, the message should be routed from header.exchange to header.queue.

A messages headers can be seen using the RabbitMQ REST API. In this example, the messages in header.queue in the foo virtual host will be returned.

curl 
--request POST 
--user john.doe:itsasecret 
--data '{"count":1,"ackmode":"ack_requeue_true","encoding":"auto","truncate":0}'
--url http://server001:15671/api/queues/foo/header.queue/get

 

The output should look something like this. Notice there is a header where the key is "foo" and the value is "bar".

[
    {
        "exchange": "header.exchange",
        "message_count": 0,
        "payload": "Hello World",
        "payload_bytes": 11,
        "payload_encoding": "string",
        "properties": {
            "delivery_mode": 2,
            "headers": {
                "foo": "bar",
                "timestamp_in_ms": 1637208962854
            },
            "timestamp": 1637208962
        },
        "redelivered": false,
        "routing_key": ""
    }
]

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 6321d in the box below so that we can be sure you are a human.