Spring Boot RabbitMQ Producer Consumer Example

SpringBoot RabbitMQ Producer Consumer Example

Tutorial: “Spring Boot RabbitMQ Producer Consumer Example”.

RabbitMQ is one of the most popular open source message broker which meets high-scale, high-availability requirements. So in the tutorial, I guide how to create Spring RabbitMQ Producer/Consumer applications with SpringBoot.

SpringBoot RabbitMQ – Spring Boot RabbitMQ Producer Consumer Example

In the tutorial, we create 2 SpringBoot applications {Producer, Consumer} for working with RabbitMQ:

SpringBoot RabbitMQ Producer & Consumer Architecture
SpringBoot RabbitMQ Producer & Consumer Architecture

– Producer will send messages to RabbitMQ Exchanges with a routingKey. RabbitMQ uses routingKey to determine which queues for routing messages.
– Consumer listens on a RabbitMQ Queue to receive messages.

With SpringBoot, we use spring.rabbitmq.* for controlling RabbitMQ configuration:


spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

– Producer uses AmqpTemplate to send messages:


@Component
public class Producer {
	
	@Autowired
	private AmqpTemplate amqpTemplate;
	
	public void produceMsg(String msg){
		amqpTemplate.convertAndSend(exchange, routingKey, msg);
	}
}

– Consumer uses @RabbitListener to recieve messages:


@Component
public class Consumer {

	@RabbitListener(queues="${jsa.rabbitmq.queue}")
    public void recievedMessage(String msg) {
        // to do
    }
}

Practice – Spring Boot RabbitMQ Example

We create 2 SpringBoot projects {Producer, Consumer}:

SpringBoot Consumer Producer Project Structure - Spring Boot RabbitMQ Producer Consumer Example
SpringBoot Consumer Producer Project Structure

Step to do:

– Create SpringBoot projects
– Create Producer/Consumer
– Setup RabbitMQ exchange, queue
– Run and check results

Create SpringBoot projects

In the tutorial “Spring Boot RabbitMQ Producer Consumer Example”, We create 2 SpringBoot projects {Producer, Consumer}, then add needed dependency:


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

Create RabbitMQ Producer

– Add RabbitMq configuration:


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

– Create RabbitMq Producer:


package com.javasampleapproach.rabbitmq.producer;

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 Producer {
	
	@Autowired
	private AmqpTemplate amqpTemplate;
	
	@Value("${jsa.rabbitmq.exchange}")
	private String exchange;
	
	@Value("${jsa.rabbitmq.routingkey}")
	private String routingKey;
	
	public void produceMsg(String msg){
		amqpTemplate.convertAndSend(exchange, routingKey, 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.javasampleapproach.rabbitmq.producer.Producer;

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

Create RabbitMQ Consumer

– Add RabbitMq configuration:


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

– Implement SpringBoot RabbitMq Consumer:


package com.javasampleapproach.rabbitmq.consumer;

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

@Component
public class Consumer {

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

Setup RabbitMQ Exchange & Queue

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

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

Rabbit Monitoring Overview - Spring Boot RabbitMQ Producer Consumer Example
Rabbit Monitoring Overview

– Add exchange:

Go to http://localhost:15672/#/exchanges.

– Add exchange: jsa.direct

Rabbitmq Add Exchange - Spring Boot RabbitMQ Producer Consumer Example
Rabbitmq Add Exchange

– Add queue:

Go to http://localhost:15672/#/queues.

– Add a queue: jsa.queue

Rabbitmq Create a Queue
Rabbitmq Create a Queue

Binding the queue with above exchange:

Binding Queue with Exchange
Binding Queue with Exchange

Run and check results

Now, it’s time for testing the tutorial “Spring Boot RabbitMQ Producer Consumer Example”:

Build and run 2 SpringBoot projects with commandlines (mvn clean install, mvn spring-boot:run).

From the SpringBoot-Producer, send a message: http://localhost:8080/send?msg=Hello World!

At the SpringBoot-Subcriber, we will get a console message: Recieved Message: Hello World!

Check connections -> go to http://localhost:15672/#/connections:

Check RabbitMQ connections
Check RabbitMQ connections

Read More – Spring Boot RabbitMQ Producer Consumer Example

Related posts:


Sourcecode

SpringRabbitMqProducer
SpringRabbitMqConsumer

Leave a Reply

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