SpringBoot RabbitMq Exchange to Exchange Topology

In the post, JavaSampleApproach will show you how to work with SpringBoot RabbitMq Exchange to Exchange Topology.

Related posts:
SpringBoot RabbitMQ Topic Exchange
SpringBoot RabbitMq Headers Exchange
RabbitMQ – How to create Spring RabbitMQ Producer/Consumer applications with SpringBoot
RabbitMq – How to create Spring RabbitMq Publish/Subcribe pattern with SpringBoot

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 Exchange to Exchange

We create a RabbitMQ topology with 2 topic exchanges and 3 binding queues:

springboot rabbitmq exchange to exchange - architecture

Scenarios with above design:
– when we send a message with routing key: sys.prod.info, it will be delivered by path: X1 -> Q3.
– when we send a message with routing key: app.prod.error, it will be delivered by path: X1 -> X2 -> {Q2, Q3}.
– when we send a message with routing key: sys.test.error, it will be delivered by path: X1 -> X2 -> Q1.

III. Practices

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

springboot rabbitmq exchange to exchange - project structures

Step to do:
– Create SpringBoot projects
– Define data model
– 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. Define data model

Create Log data model for both projects:

3. Implement RabbitMq producer
3.1 Configure RabbitMq producer

Open application.properties, add configuration:

3.2 Implement RabbitMq producer

3.3 Implement RabbitMqProducer client

4. Implement RabbitMq consumer
4.1 Configure RabbitMq consumer

Open application.properties, add configuration:

4.2 Implement consumer

5. Run and check results
5.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 exchanges:
Go to http://localhost:15672/#/exchanges, add 2 exchanges: {jsa.exchange.logs, jsa.exchange.logs.error}.

springboot rabbitmq exchange to exchange - create 2 exchange

Add queue:
Go to http://localhost:15672/#/queues, add 3 queues: {jsa.queue.logs.application-error, jsa.queue.logs.system-error, jsa.queue.logs.production}.

springboot rabbitmq exchange to exchange

– Binding the queues & exchanges:

springboot rabbitmq exchange to exchange - binding to error queues

springboot rabbitmq exchange to exchange - binding to exchange

5.2 Run & check results

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

-> Console’s logs:

-> See queues’ status:

springboot rabbitmq exchange to exchange - results in queue

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

-> Console’s logs:

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

-> Console’s logs:

– Run SpringBootRabbitMqConsumer which listen to jsa.queue.logs.production queue with configuration:
jsa.rabbitmq.queue=jsa.queue.logs.production:

-> Console’s logs:

IV. Sourcecode

SpringRabbitMqConsumer
SpringRabbitMqProducer

By JavaSampleApproach | October 14, 2017.


Related Posts


Got Something To Say:

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

*