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

– 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:

package com.javasampleapproach.rabbitmq.publisher;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

public class Publisher {
	private AmqpTemplate amqpTemplate;
	private String exchange;
	public void produceMsg(String msg){
		amqpTemplate.convertAndSend(exchange, "",msg);
		System.out.println("Send msg = " + msg);

– Implement a sending restApi:

package com.javasampleapproach.rabbitmq.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.javasampleapproach.rabbitmq.publisher.Publisher;

public class WebController {
	Publisher publisher;
	public String sendMsg(@RequestParam("msg")String msg){
		return "Done";

2.2 Create Subcriber

– Add RabbitMq configuration:


– Create RabbitMq Subcriber:

package com.javasampleapproach.rabbitmq.subcriber;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

public class Subcriber {

    public void recievedMessage(String msg) {
        System.out.println("Recieved Message: " + msg);

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


4 thoughts on “RabbitMq – How to create Spring RabbitMq Publish/Subcribe pattern with SpringBoot”

  1. I have learned some new points from your web-site about desktops. Another thing I have always imagined is that computers have become an item that each home must have for many reasons. They supply you with convenient ways to organize households, pay bills, search for information, study, hear music and in many cases watch tv shows. An innovative strategy to complete these tasks is to use a mobile computer. These pc’s are mobile ones, small, robust and convenient.

  2. 899414 838374Hello. Cool article. Theres an issue with the internet site in internet explorer, and you may want to test this The browser could be the marketplace chief and a large element of other folks will miss your wonderful writing due to this dilemma. 830169

Leave a Reply

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