Spring AMQP Exchange Types

AMQP is stands for Advanced Messaging Queuing Protocol is an open source published and standard protocol for publishing message oriented middleware like Java Messaging system. JMS is more specific to communicate between only Java applications, where AMQP is used to communicate with any Java application developed in any language.

AMQP provides a way to communicate between the applications or organizations in an encrypted and interoperable way. It also provide the loose coupling between application and integrate easily with any number of applications, As it is independent of the type of application, and it can also support for mobile applications.

It can also used in IOT devices, like one application will sent the data other application using AMQP, and other application will react based on data sent by first application.

The main features of AMQP are:

Ubiquity: Open Internet protocol standard, supporting easy to use, implement and extensions.
Safety: It provides secured way to exchange messages, supports message durability and in case of failure it sent the failure delivery to sender.

Interoperability: message exchanging, queuing, routing (including point-to-point and publish-and-subscribe).

RabbitMQ is one of the open source message broker, implemented the AMQP, it provide the plug-in architecture to support Streaming Text Oriented Messaging Protocol (STOMP), and Message Queuing Telemetry Transport (MQTT).

Spring-AMQP is one of the module in Spring, which provides an easy to communicate using spring framework.
 
Exchange Types:

Exchanges controls the routing messages to queue based on binding keys. Each Exchange Type has a specific routing algorithm which underlying AMQP server used to determine which bounded queues to publish the incoming messages.

Exchanges will take the messages, based on exchange type each message will route to zero or more queues.

RabbitMQ is one of the most used AMQP implementation is supporting below 

Exchange types

Exchange Type Default pre-declared names
Direct Exchange amq.direct or (empty string)
Fanout Exchange amq.fanout
Topic Exchange amq.topic
Headers Exchange amq.headers


Other exchange types, exchanges are declared with number of other attributes like:
  1. Name
  2. Durability (Exchanges presence after restart)
  3. Auto-delete (auto delete exchange when last queue is unbound from it)
  4. Arguments (optional, most used by plugins, to specify plugin related features)

Default Exchange: The default Exchange is the default exchange with no name,pre-declared by broker.


 
When we are sending message with routing key, binding key will be equal to binding key, based on these binding key, the message will be passed to queue.

The sender will send the messages to same Direct Exchange with different key, based on this key we can message will route the different queues.
based on this routing key we can categories the types of messages, if we need to distinguish between messages.

If the routing key will matches to queue name, it rabbitmq will forward message to queue.  For this type of exchanges, default exchange will be used, all are queues are automatically bound to default exchange.

This default exchange is declared as a Direct exchange.

Fanout Exchange: The fanout Exchange type route the message all bound queues. If message has routing key, it will be ignored. 



The Fanout exchange type is useful for implementing the publish-subscribe pattern.  As Fanout Exchnage type, will not depend on routing key, it will send all messages to all queues, which are attached to exchange. For example, if a customer order on product, it has sent the product details with inventory, and order application.

This exchange type is used when one application want to send the data to more number of applications mandatory. In java world we can think like Observer design pattern, if sender will send the message, all observers will get notified, without any conditions.

Topic Exchange: Topic Exchange is almost like direct Exchange, with one difference. Messages will hold the routing key, these topic exchanges will hold routing key with regular expression, if the routing key matches either partially or fully then message will route to the queue otherwise not.


 
The Topic exchange is powerful and it can behave like either fanout or direct exchange based on routing key pattern.

  1. When queue is bounded with “#”(hash) binding key, then all messages from sender will send to that queue. It is same as fanout exchange.
  2. If the queue is not bound with either *(star) or #(hash) then if the routing key matches with queue bounded-key then only message will be sent to queue. It is same as Direct Exchange.

Characters used in Topic Exchange routing key:
  1. “#”(hash) : can substitute with zero or more words.
  2. “*”(star):  can substitute with exactly one word.

Headers Exchange: The Header interchange will be construct on message headers not routing key. Header interchanges are very close to topic changes, but route messages are construct on header values not using routing key.

A special argument “x-match” is added between either all messages headers should match or any one of the header match is sufficient to send the message.

“x-match”property accepts two values.

  1. all: if “x-match” value is “all” then all message headers should match with queue binding, then only message will forward to it.
  2. any: if “x-match” value is “any” then any one of the message headers  matches with queue binding, then only message will forward to it.


Conclusion: We learn the different types of Exchanges in AMQP in detail, and where we can use these individual exchanges.