SpringBoot RabbitMQ Topic Exchange
Tags: springboot, rabbitmq, topic, exchange
Published: 2021-05-05 17:47:36

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

SpringBoot RabbitMQ Topic Exchange

In the past post, we had introduced about <a href="https://grokonez.com/java-integration/distributed-system/rabbitmq-create-spring-rabbitmq-publishsubcribe-pattern-springboot">RabbitMQ Publish/Subcribe</a> pattern with fanout exchange. Today, <a href="https://grokonez.com">JavaSampleApproach</a> will show you how to work with <strong>SpringBoot RabbitMQ Topic Exchange</strong>.

Related posts:
- <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/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/java-integration/distributed-system/rabbitmq-create-spring-rabbitmq-publishsubcribe-pattern-springboot#4_Run_and_check_results">RabbitMq – How to create Spring RabbitMq Publish/Subcribe pattern with SpringBoot</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/springboot-rabbitmq-headers-exchange">SpringBoot RabbitMq Headers Exchange</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 Topic Exchange</h2>
<code>routing_key</code> of messages sent to a <strong>topic exchange</strong> must be a list of words, delimited by dots, example:

- <code>#.error</code>
- <code>*.prod.*</code>
- <code>sys.#</code>

<strong>Note</strong>:
<strong>*</strong> (star) must be an exactly one word.
<strong>#</strong> (hash) can be zero or more words.

<img src="https://grokonez.com/wp-content/uploads/2017/10/springboot-rabbitmq-topic-architecture-1.png" alt="springboot rabbitmq topic - architecture" width="668" height="233" class="alignnone size-full wp-image-8429" />

With the above topic exchange design,
- when we send a message with routing key: <code>sys.dev.info</code>, it will just be delivered to <strong>Q1</strong>.
- when we send a message with routing key: <code>app.prod.error</code>, it will just be delivered to <strong>Q2</strong>.
- when we send a message with routing key: <code>sys.test.error</code>, it will be delivered to both queues {<strong>Q1</strong>, <strong>Q2</strong>}.

<div class="jsa-quote">
Topic exchange is strong tool and it can act as other exchanges as below:

- When a queue is bound with "#" (hash) binding key - it is as an fanout exchange.
- When don't use <strong>*</strong> & <strong>#</strong> in bindings, it will behave as a direct exchange.
</div>
<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-topic-project-structure.png" alt="springboot rabbitmq topic -project structure" width="672" height="275" class="alignnone size-full wp-image-8399" />

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

SpringBoot RabbitMQ Topic Exchange