Exchanges An Exchange is a named entity within the Virtual Host which receives messages from producers and routes them to matching Queues within the Virtual Host. The server provides a set of exchange types with each exchange type implementing a different routing algorithm. For details of how these exchanges types work see below. The server predeclares a number of exchange instances with names starting with "amq.". These are defined in . Applications can make use the pre-declared exchanges, or they may declare their own. The number of exchanges within a virtual host is limited only by resource constraints. The behaviour when an exchange is unable to route a message to any queue is defined in Exchange configuration is covered in .
Predeclared Exchanges Each virtual host pre-declares the following exchanges: amq.direct (an instance of a direct exchange) amq.topic (an instance of a topic exchange) amq.fanout (an instance of a fanout exchange) amq.match (an instance of a headers exchange) The conceptual "default exchange" always exists, effectively a special instance of direct exchange which uses the empty string as its name. All queues are automatically bound to it upon their creation using the queue name as the binding key, and unbound upon their deletion. It is not possible to manually add or remove bindings within this exchange. Applications may not declare exchanges with names beginning with "amq.". Such names are reserved for system use.
Exchange Types The following Exchange types are supported. Direct Topic Fanout Headers These exchange types are described in the following sub-sections.
Direct The direct exchange type routes messages to queues based on an exact match between the routing key of the message, and the binding key used to bind the queue to the exchange. This exchange type is often used to implement point to point messaging. When used in this manner, the normal convention is that the binding key matches the name of the queue. It is also possible to use this exchange type for multi-cast, in this case the same binding key is associated with many queues.
Direct exchange
The figure above illustrates the operation of direct exchange type. The yellow messages published with the routing key "myqueue" match the binding key corresponding to queue "myqueue" and so are routed there. The red messages published with the routing key "foo" match two bindings in the table so a copy of the message is routed to both the "bar1" and "bar2" queues. The routing key of the blue message matches no binding keys, so the message is unroutable. It is handled as described in .
Topic This exchange type is used to support the classic publish/subscribe paradigm. The topic exchange is capable of routing messages to queues based on wildcard matches between the routing key and the binding key pattern defined by the queue binding. Routing keys are formed from one or more words, with each word delimited by a full-stop (.). The pattern matching characters are the * and # symbols. The * symbol matches a single word and the # symbol matches zero or more words. The topic exchange is also capable of routing messages according to whether a message's header values or properties match a JMS message selector This is a Qpid specific extension.. The following three figures help explain how the topic exchange functions.
Topic exchange - exact match on topic name
The figure above illustrates publishing messages with routing key "weather". The exchange routes each message to every bound queue whose binding key matches the routing key. In the case illustrated, this means that each subscriber's queue receives every yellow message.
Topic exchange - matching on hierarchical topic patterns
The figure above illustrates publishing messages with hierarchical routing keys. As before, the exchange routes each message to every bound queue whose binding key matches the routing key but as the binding keys contain wildcards, the wildcard rules described above apply. In the case illustrated, sub1 has received the red and green message as "news.uk" and "news.de" match binding key "news.#". The red message has also gone to sub2 and sub3 as it's routing key is matched exactly by "news.uk" and by "*.uk". The routing key of the yellow message matches no binding keys, so the message is unroutable. It is handled as described in .
Topic exchange - matching on JMS message selector
The figure above illustrates messages with properties published with routing key "shipping". As before, the exchange routes each message to every bound queue whose binding key matches the routing key but as a JMS selector argument has been specified, the expression is evaluated against each matching message. Only messages whose message header values or properties match the expression are routed to the queue. In the case illustrated, sub1 has received the yellow and blue message as their property "area" cause expression "area in ('Forties', 'Cromarty')" to evaluate true. Similarly, the yellow message has also gone to gale_alert as its property "speed" causes expression "speed > 7 and speed < 10" to evaluate true. The properties of purple message cause no expressions to evaluate true, so the message is unroutable. It is handled as described in .
Fanout The fanout exchange type routes messages to all queues bound to the exchange, regardless of the message's routing key.
Fanout exchange
Headers The headers exchange type routes messages to queues based on header properties within the AMQP message. The message is passed to a queue if the header properties of the message satisfy the header matching arguments table with which the queue was bound.
Unrouteable Messages If an exchange is unable to route a message to any queues, the Broker will: If using AMQP 0-10 protocol, and an alternate exchange has been set on the exchange, the message is routed to the alternate exchange. The alternate exchange routes the message according to its routing algorithm and its binding table. If the messages is still unroutable, the message is discarded. If using AMQP protocols 0-8..0-9-1, and the publisher set the mandatory flag and the close when no route feature is disabled, the message is returned to the Producer. Otherwise, the message is discarded.