SpringBoot RabbitMq Headers Exchange

In the past post, we had introduced about RabbitMQ Topic Exchange. Today, JavaSampleApproach will show you how to work with SpringBoot RabbitMQ Headers Exchange.

Related posts:
SpringBoot RabbitMQ Topic Exchange
RabbitMQ – How to create Spring RabbitMQ Producer/Consumer applications with SpringBoot
RabbitMq – How to create Spring RabbitMq Publish/Subcribe pattern with SpringBoot
RabbitMQ – How to send/receive Java object messages with Spring RabbitMq | SpringBoot
SpringBoot RabbitMq Exchange to Exchange Topology

I. Technologies

– Java 8
– Maven 3.6.1
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.7.RELEASE
– RabbitMQ

II. RabbitMq Headers Exchange

Headers exchange routes message based on header values instead of routing keys.
A special argument named x-match has 2 values {all, any} where all is the default value of a headers binding.
x-match = all means that all the values must match.
x-match = any means just one matching header value is sufficient.

springboot rabbitmq exchage headers - architecture design

Scenarios with above design:
– When sending a message with headers: {layer=system, level=error}, the message will be delivered to 2 queues {Q1, Q2}.
– When sending a message with headers: {layer=application, level=error}, the message will be delivered to one queue Q2.
– When sending a message with headers: {layer=system, level=info}, the message will be discarded.

III. Practices

In the tutorial, we create 2 SpringBoot project as below:

springboot rabbitmq exchage headers - project structures

Step to do:
– Create SpringBoot projects
– Implement RabbitMq producer
– Implement RabbitMq consumer
– Run and check results

1. Create SpringBoot projects

Using SpringToolSuite, create 2 SpringBoot projects, then add need dependency spring-boot-starter-amqp:

2. Implement RabbitMq producer
2.1 Configure RabbitMq connection

Open application.properties file, add configuration:

2.2 Implement Producer

2.3 Implement Producer client

3. Implement RabbitMq consumer
3.1 Configure RabbitMq connection

Open application.properties file, add configuration:

3.2 Implement Consumer

4. Run and check results
4.1 Setup RabbitMq exchange, queues

Enable rabbitmq_management by cmd: rabbitmq-plugins enable rabbitmq_management --online
Then go to: http://localhost:15672 -> login with user/password: guest/guest.

Add exchange:
Go to http://localhost:15672/#/exchanges, add exchange: jsa.exchange.logs.headers

springboot rabbitmq exchage headers - create exchange

Add queue:
Go to http://localhost:15672/#/queues, add 2 queues: jsa.queue.logs.all-sys-error, jsa.queue.logs.any-app-error.

springboot rabbitmq exchage headers - create queues

– Binding the queues with above exchange:

springboot rabbitmq exchage headers - binding queues with exchange

4.2 Run & check results

– Run SpringBootRabbitMqProducer with commandline mvn spring-boot:run,

-> Console’s logs:

-> See queues’ status:

springboot rabbitmq exchage headers - after sending messages

-> jsa.queue.logs.all-sys-error queue has 1 message:

get messages of jsa.queue.logs.all-sys-error

-> jsa.queue.logs.any-app-error queue has 2 messages:

get messages of jsa.queue.logs.any-app-error

– Run SpringBootRabbitMqConsumer which listen to jsa.queue.logs.all-sys-error queue with configuration: jsa.rabbitmq.queue=jsa.queue.logs.all-sys-error:

-> Console’s logs:

– Run SpringBootRabbitMqConsumer which listen to jsa.queue.logs.any-app-error queue with configuration: jsa.rabbitmq.queue=jsa.queue.logs.any-app-error:

-> Console’s logs:

IV. Sourcecode

SpringRabbitMqProducer
SpringRabbitMqConsumer

By JavaSampleApproach | October 11, 2017.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*