RabbitMQ Publish/Subscribe Java Example + SpringBoot

– Tutorial “Rabbitmq Publish/Subscribe Java Example with SpringBoot”

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

Spring RabbitMq Publish/Subcribe pattern – RabbitMQ Publish/Subscribe Java Example + SpringBoot

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 - RabbitMQ Publish/Subscribe Java Example + SpringBoot
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.

Practice – Rabbitmq Publish/Subscribe Java Example + SpringBoot

* Technologies:

– Java 8
– Maven 3.6.1
– Spring Tool Suite
– Spring Boot
– RabbitMQ

We create 2 SpringBoot projects {Publisher, Subcriber}:

Spring RabbitMQ Publish Subscribe Pattern Project Structures - RabbitMQ Publish/Subscribe Java Example + SpringBoot
Spring RabbitMQ Publish Subscribe Pattern Project Structures

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

Create SpringBoot projects – RabbitMQ Publish/Subscribe Java Example + SpringBoot

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


<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

– With Spring-Rabbit-MQ-Publiser, we add more web dependency for creating RestController:


<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Create SpringBoot RabbitMQ Publisher – RabbitMQ Publish/Subscribe Java Example + SpringBoot

– Add RabbitMq configuration:


spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
jsa.rabbitmq.exchange=jsa.fanout

– Implement RabbitMQ Publisher:


package com.loizenai.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;

@Component
public class Publisher {
	
	@Autowired
	private AmqpTemplate amqpTemplate;
	
	@Value("${jsa.rabbitmq.exchange}")
	private String exchange;
	
	public void produceMsg(String msg){
		amqpTemplate.convertAndSend(exchange, "",msg);
		System.out.println("Send msg = " + msg);
	}
}

– Implement a sending RestAPI:


package com.loizenai.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.loizenai.rabbitmq.publisher.Publisher;

@RestController
public class WebController {
	
	@Autowired
	Publisher publisher;
	
	@RequestMapping("/send")
	public String sendMsg(@RequestParam("msg")String msg){
		publisher.produceMsg(msg);
		return "Done";
	}
}

Create Spring RabbitMQ Subcriber

– Add RabbitMq configuration:


spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
jsa.rabbitmq.queue=jsa.queue.1

– Create RabbitMQ Subcriber:


package com.loizenai.rabbitmq.subcriber;

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

@Component
public class Subcriber {

	@RabbitListener(queues="${jsa.rabbitmq.queue}")
    public void recievedMessage(String msg) {
        System.out.println("Recieved Message: " + msg);
    }
}

Setup RabbitMQ Exchange, Queues – RabbitMQ Publish/Subscribe Java Example + SpringBoot

Enable rabbitmq_management by cmd: rabbitmq-plugins enable rabbitmq_management --online

Go to: http://localhost:15672. Then login with user/password: guest/guest.

– Add an RabbitMQ Exchange:

Create a fanout exchange: jsa.fanout

Spring RabbitMQ Publish Subscribe Pattern Create Exchanges - RabbitMQ Publish/Subscribe Java Example + SpringBoot
Spring RabbitMQ Publish Subscribe Pattern Create Exchanges

– Add RabbitMQ Queues:

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

Spring RabbitMQ Publish Subscribe Pattern Create 3 Queues
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
Spring RabbitMQ Publish Subscribe Pattern Binding Queue with Exchanges

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
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
Spring RabbitMQ Publish Subscribe Pattern Consumer Messages

– All messages has been consumed by Subcribers.

Read More

Related posts:


– Reference Link: RabbitMQ Publish/Subscribe

SourceCode – RabbitMQ Publish/Subscribe Java Example + SpringBoot

SpringBoot-RabbitMQ-Publisher
SpringBoot-RabbitMQ-Subcriber