This topic describes the exchange concept and exchange types to help you better understand and select exchanges in Message Queue for RabbitMQ.

What is an exchange?

An exchange is a message routing agent of Message Queue for RabbitMQ. When a producer sends a message to the Message Queue for RabbitMQ broker, the message is first sent to an exchange. Then, the exchange routes the message to one or more queues. An exchange routes messages based on binding keys, routing keys, and headers attributes.

Direct exchange

  • Routing rule

    A direct exchange routes a message to the queue whose binding key exactly matches the routing key of the message.

  • Scenarios

    A direct exchange is suitable for scenarios in which messages are distinguished by simple strings. In most cases, a direct exchange is used for the unicast routing of messages.

  • Example

    The following figure shows an example.

    direct_exchange
    Message Routing key Binding key Queue
    Message A img.create img.create Queue A
    Message B img.log img.log Queue B

Topic exchange

  • Routing rule

    A topic exchange routes a message to the queues whose binding keys partially match the routing key of the message. An asterisk (*) or number sign (#) is used as a wildcard in a binding key to support partial match. An asterisk (*) represents a word, such as cn. A number sign (#) represents zero, one, or more words that are separated by periods (.), such as cn.zj.hz.

  • Scenarios

    A topic exchange is suitable for scenarios in which messages are distinguished by wildcards. In most cases, a topic exchange is used for the multicast routing of messages. For example, you can use a topic exchange to distribute geographic location data.

  • Example

    The following figure shows an example.

    topic_exchange
    Message Routing key Binding key Queue
    Message A files.cn.hz files.cn.hz. # Queue A
    Message B files.cn.hz.store
    • files.cn.hz. #
    • files.cn. *.store
    • Queue A
    • Queue B
    Message C files.cn.sz.store files.cn. *.store Queue B

Fanout exchange

  • Routing rule

    A fanout exchange routes messages to all of the queues that are bound to it. Routing keys and binding keys are ignored.

  • Scenarios

    A fanout exchange is ideal for the broadcast routing of messages. For example, you can use a fanout exchange in a distribution system to broadcast status or configuration updates.

  • Example

    The following figure shows an example.

    fanout_exchange
    Message Routing key Binding key Queue
    Message A img.create
    • files.cn.hz. #
    • queue.msgText
    • Queue A
    • Queue B
    Message B queue.msgMap
    • files.cn.hz. #
    • queue.msgText
    • Queue A
    • Queue B
    Message C cn.hz.topic.alarm
    • files.cn.hz. #
    • queue.msgText
    • Queue A
    • Queue B

Headers exchange

  • Routing rule

    A headers exchange can be considered as a special direct exchange. It works in a similar way to direct exchanges. The difference lies in that a headers exchange uses headers attributes to match binding attributes, instead of using routing keys to match binding keys. When you bind a headers exchange to a queue, you must configure binding attributes in the key-value format for the binding. When you send a message to a headers exchange, you must configure headers attributes in the key-value format for the message. After a headers exchange receives a message, it routes the message based on the matching between the headers attributes of the message and binding attributes of the bound queues.

    A special binding attribute, x-match, is used to determine the matching method. x-match can be set to all or any:

    • all: A headers exchange routes a message to a queue only if all binding attributes of the queue except for x-match match the headers attributes of the message.
    • any: A headers exchange routes a message to a queue if one or more binding attributes of the queue except for x-match match the headers attributes of the message.

    A headers attribute is considered to match a binding attribute in the following scenarios:

    • The key and value of the headers attribute are exactly the same as those of the binding attribute.
    • The key of the headers attribute is exactly the same as that of the binding attribute, but the value of the binding attribute is empty.
  • Scenarios

    A headers exchange is suitable for scenarios in which messages are distinguished by multiple headers attributes. In most cases, a headers exchange is used for the multicast routing of messages. For example, you can use a headers exchange to distribute news that is classified or tagged.

  • Example

    The following figure shows an example.

    headers_exchange
    Message Headers attribute Binding attribute Queue
    Message A
    type=read
    resource=group
    • type=read
      resource=group
      x-match=all
    • type=read
      resource=topic
      x-match=any
    • Queue A
    • Queue B
    Message B
    type=read
    type=read
    resource=topic
    x-match=any
    Queue B
    Message C
    type=write
    resource=topic
    type=read
    resource=topic
    x-match=any
    Queue B

JMS queue exchange

In addition to AMQP exchanges, Message Queue for RabbitMQ also supports JMS queue exchanges. In essence, a JMS queue exchange is a direct exchange. Message Queue for RabbitMQ uses JMS queue exchanges to support JMS queues.

  • Routing rule

    A JMS queue exchange routes a message to the queue whose binding key exactly matches the routing key of the message.

  • Scenarios

    A JMS queue exchange is suitable for scenarios in which messages are distinguished by simple strings. In most cases, a JMS queue exchange is used for the unicast routing of messages.

  • Example

    The following figure shows an example.

    jms_queue_exchange
    Message Routing key Binding key Queue
    Message A queue.msgText queue.msgText Queue A
    Message B queue.msgMap queue.msgMap Queue B
    Message C queue.msgObj queue.msgObj Queue C

JMS topic exchange

In addition to AMQP exchanges, Message Queue for RabbitMQ also supports JMS topic exchanges. In essence, a JMS topic exchange is a topic exchange. Message Queue for RabbitMQ uses JMS topic exchanges to support JMS topics.

  • Routing rule

    A JMS topic exchange routes a message to the queues whose binding keys partially match the routing key of the message. An asterisk (*) or number sign (#) is used as a wildcard in a binding key to support partial match. An asterisk (*) represents a word, such as cn. A number sign (#) represents zero, one, or more words that are separated by periods (.), such as cn.zj.hz.

  • Scenarios

    A JMS topic exchange is suitable for scenarios in which messages are distinguished by wildcards. In most cases, a JMS topic exchange is used for the multicast routing of messages. For example, you can use a JMS topic exchange to distribute geographic location data.

  • Example

    The following figure shows an example.

    jms_topic_exchange
    Message Binding key Routing key Queue
    Message A cn.hz.instance cn.hz.instance. # Queue A
    Message B cn.hz.instance.alarm
    • cn.hz.instance. #
    • cn.hz. *.alarm
    • Queue A
    • Queue B
    Message C cn.hz.topic.alarm cn.hz. *.alarm Queue B