This topic introduces different types of exchanges 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 can be used in scenarios where 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 a number sign (#) is used as a wildcard in a binding key to support partial match. An asterisk (*) represents a word. An example is cn. A number sign (#) represents zero, one, or more words that are separated by periods (.). An example is cn.zj.hz.

  • Scenarios

    A topic exchange can be used in scenarios where 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 routes messages to queues based on headers attributes instead of routing 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 is used to determine the matching method. This binding attribute is x-match. The value of 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 the same as those of the binding attribute.
    • The key of the headers attribute is the same as that of the binding attribute, but the value of the binding attribute is empty.
  • Scenarios

    A headers exchange can be used in scenarios where 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 Java Message Service (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.

Note You can create a JMS queue exchange only by using the SDK.
  • 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 can be used in scenarios where 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 a number sign (#) is used as a wildcard in a binding key to support partial match. An asterisk (*) represents a word. An example is cn. A number sign (#) represents zero, one, or more words that are separated by periods (.). An example is cn.zj.hz.

  • Scenarios

    A JMS topic exchange can be used in scenarios where 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

x-delayed-message exchange

Message Queue for RabbitMQ is also compatible with x-delayed-message exchanges of open source RabbitMQ. Open source RabbitMQ allows you to use x-delayed-message exchanges by installing the delayed message plug-in. To use x-delayed-message exchanges in Message Queue for RabbitMQ, you do not need to install the plug-in. You need only to declare an exchange of the x-delayed-message type and set the x-delay header attribute of the message to specify the period of time after which the message will be delivered. The period of time is in milliseconds. The message will be delivered to the corresponding queue after the period of time specified by the x-delay header attribute.

  • Routing rule
    An x-delayed-message exchange determines the routing rule based on the exchange type specified by the x-delayed-type extended attribute. The following types of exchanges support the x-delayed-message type:
  • Scenarios

    An x-delayed-message exchange can be used in scenarios where messages need to be delivered after a specified delay. For more information, see Delayed messages.

  • Example

    An x-delayed-message exchange routes messages based on the routing rule of the exchange type specified by the x-delayed-type extended attribute. Assume that x-delayed-type is set to direct. The following figure shows an example.

    x-delayed-message-exchange
    Message x-delay Routing key Binding key Queue
    Message A 3000 img.create img.create Queue A
    Message B 4000 img.log img.log Queue B
    The following information describes how the x-delayed-message exchange routes Message A and Message B:
    • After the x-delayed-message exchange receives Message A, the x-delayed-message exchange delivers Message A to Queue A after 3,000 milliseconds.
    • After the x-delayed-message exchange receives Message B, the x-delayed-message exchange delivers Message B to Queue B after 4,000 milliseconds.