RabbitMq – How to create Spring RabbitMq Publish/Subcribe pattern with SpringBoot

In the tutorial, JavaSampleApproach will guide how to create Spring RabbitMq Publish/Subcribe pattern by using fanout exchanges of RabbitMq and SpringBoot.

Related articles:
RabbitMQ – How to create Spring RabbitMQ Producer/Consumer applications with SpringBoot
How to start Spring Kafka Application with Spring Boot
RabbitMq Queue Durability and Persistent MessageDelivery | SpringBoot
RabbitMQ – How to send/receive Java object messages with Spring RabbitMq | SpringBoot
SpringBoot RabbitMQ Topic Exchange
SpringBoot RabbitMq Headers Exchange


I. Spring RabbitMq Publish/Subcribe pattern

We create a Publisher, and 3 Subcribers and using fanout exchanges of RabbitMQ for create a Publish/Subcribe Pattern System:

Spring RabbitMq Publish–Subscribe pattern - architechture

Flow messages:
Publisher will send messages to the fanout exchange.
– The fanout exchange routes messages to all of the queues that are bound to it and the routing key is ignored.
Subcribers instances recieves messages from the queues.

II. Practice

Technologies
– Java 8
– Maven 3.6.1
– Spring Tool Suite: Version 3.8.4.RELEASE
– Spring Boot: 1.5.4.RELEASE
– RabbitMQ

We create 2 SpringBoot projects {Publisher, Subcriber}:

Spring RabbitMq Publish–Subscribe pattern - project structures

Step to do:
– Create SpringBoot projects
– Create Publiser/Subcriber
– Setup RabbitMQ exchange, queues
– Run and check results

1. Create SpringBoot projects

Using Spring Tool Suite, create 2 Spring Starter Projects then add amqp dependency for both of them:

– With SpringRabbitMqPubliser we add more web dependency for creating RestController:

2. Create Publiser/Subcriber
2.1 Create Publisher

– Add RabbitMq configuration:

– Implement RabbitMq Publisher:

– Implement a sending restApi:

2.2 Create Subcriber

– Add RabbitMq configuration:

– Create RabbitMq Subcriber:

3. Setup RabbitMQ exchange, queues

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

3.1 Add an exchange

Create a fanout exchange: jsa.fanout

Spring RabbitMq Publish–Subscribe pattern - create exchanges

3.2 Add queues

Create 3 RabbitMq queues {jsa.queue.1, jsa.queue.2, jsa.queue.3}:

Spring RabbitMq Publish–Subscribe pattern - create 3 queues

Binding all above queues with the fanout exchange jsa.fanout:

Spring RabbitMq Publish–Subscribe pattern - binding queue with exchanges - exchange sige

4. Run and check results

Build 2 SpringBoot projects {Publisher, Subcriber} with the commandline: mvn clean install.
Run Publisher with commandline: mvn spring-boot:run, then send a message by a request: localhost:8080/send?msg=Hello World!

-> See status of the Queues:

Spring RabbitMq Publish–Subscribe pattern - queue status after send first message

Run 3 Subcriber instances which different configured values of jsa.rabbitmq.queue: {jsa.queue.1, jsa.queue.2, jsa.queue.3}.
See console logs of each Subcriber instances, we got the same message: ‘Recieved Message: Hello World!’.

-> Again, check status of the queues:

Spring RabbitMq Publish–Subscribe pattern - consumer messages

-> All messages has been consumed by Subcribers.

III. Sourcecode

SpringRabbitMqPublisher
SpringRabbitMqSubcriber

By JavaSampleApproach | July 11, 2017.

Last updated on October 16, 2017.


Related Posts


Got Something To Say:

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

*