SpringBoot RabbitMq Headers Exchange
Tags: springboot, rabbitmq, header, exchange
Published: 2021-04-25 11:18:58

https://grokonez.com/spring-framework/spring-amqp/springboot-rabbitmq-headers-exchange

SpringBoot RabbitMq Headers Exchange

In the past post, we had introduced about <a href="https://grokonez.com/spring-framework/spring-boot/springboot-rabbitmq-topic-exchange">RabbitMQ Topic Exchange</a>. Today, <a href="https://grokonez.com">JavaSampleApproach</a> will show you how to work with <strong>SpringBoot RabbitMQ Headers Exchange</strong>.

Related posts:
- <a href="https://grokonez.com/spring-framework/spring-amqp/springboot-rabbitmq-topic-exchange">SpringBoot RabbitMQ Topic Exchange</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/create-spring-rabbitmq-producerconsumer-application-springboot">RabbitMQ – How to create Spring RabbitMQ Producer/Consumer applications with SpringBoot</a>
- <a href="https://grokonez.com/java-integration/distributed-system/rabbitmq-create-spring-rabbitmq-publishsubcribe-pattern-springboot">RabbitMq – How to create Spring RabbitMq Publish/Subcribe pattern with SpringBoot</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/rabbitmq-sendreceive-java-object-messages-spring-rabbitmq-springboot">RabbitMQ – How to send/receive Java object messages with Spring RabbitMq | SpringBoot</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/springboot-rabbitmq-exchange-exchange-topology">SpringBoot RabbitMq Exchange to Exchange Topology</a>

<!--more-->
<h2>I. Technologies</h2>
– Java 8
– Maven 3.6.1
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.7.RELEASE
– RabbitMQ
<h2>II. RabbitMq Headers Exchange</h2>
<strong>Headers exchange</strong> routes message based on header values instead of routing keys.
A special argument named <code>x-match</code> has 2 values {<code>all</code>, <code>any</code>} where <code>all</code> is the default value of a headers binding.
- <code>x-match</code> = <strong>all</strong> means that all the values must match.
- <code>x-match</code> = <strong>any</strong> means just one matching header value is sufficient.

<img src="https://grokonez.com/wp-content/uploads/2017/10/springboot-rabbitmq-exchage-headers-architecture-design.png" alt="springboot rabbitmq exchage headers - architecture design" width="653" height="224" class="alignnone size-full wp-image-8443" />

Scenarios with above design:
- When sending a message with headers: {<code>layer=system</code>, <code>level=error</code>}, the message will be delivered to 2 queues {<strong>Q1</strong>, <strong>Q2</strong>}.
- When sending a message with headers: {<code>layer=application</code>, <code>level=error</code>}, the message will be delivered to one queue <strong>Q2</strong>.
- When sending a message with headers: {<code>layer=system</code>, <code>level=info</code>}, the message will be discarded.
<h2>III. Practices</h2>
In the tutorial, we create 2 SpringBoot project as below:

<img src="https://grokonez.com/wp-content/uploads/2017/10/springboot-rabbitmq-exchage-headers-project-structures.png" alt="springboot rabbitmq exchage headers - project structures" width="684" height="200" class="alignnone size-full wp-image-8444" />

<strong>Step to do</strong>:
– Create SpringBoot projects
– Implement RabbitMq producer
– Implement RabbitMq consumer
– Run and check results
<h3>1. Create SpringBoot projects</h3>
Using <strong>SpringToolSuite</strong>, create 2 <strong>SpringBoot</strong> projects, then add need dependency <code>spring-boot-starter-amqp</code>:

https://grokonez.com/spring-framework/spring-amqp/springboot-rabbitmq-headers-exchange

SpringBoot RabbitMq Headers Exchange