Spring Boot RabbitMQ Example
In this tutorial, learn step by step how to develop and deploy Spring Boot with Local RabbitMQ.
RabbitMQ Tutorial :
Note : The full source code for spring boot rabbitMQ example can be downloaded at the end of this article.
- To begin with, first Create Spring Boot Project from Spring Initializer site https://start.spring.io/.
- Project will look as given below
- Add RabbitMq dependency in pom.xml
- Provide rabbitmq properties in application.properties
- Create Configuration class to connect to RabbitMQ.
- Create controller class to expose the endpoint to send message to RabbitMQ.
- Create Message Producer/Sender, which send messages to RabbitMQ Consumer using Direct Exchange as shown below.
- Now, test this application at local. Start local RabbitMQ and Run Spring Boot App
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.techgeeknext</groupId>
<artifactId>SpringBootRabbitMQEaxmple</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootRabbitMQEaxmple</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-cloud-connectors -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cloud-connectors</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
techgeeknext.rabbitmq.exchange=techgeeknext.exchange
techgeeknext.rabbitmq.queue=techgeeknext.queue
techgeeknext.rabbitmq.routingkey=techgeeknext.routingkey
package com.techgeeknext.config;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
@Value("${techgeeknext.rabbitmq.queue}")
String queueName;
@Value("${techgeeknext.rabbitmq.exchange}")
String exchange;
@Value("${techgeeknext.rabbitmq.routingkey}")
private String routingkey;
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
DirectExchange exchange() {
return new DirectExchange(exchange);
}
@Bean
Binding binding(Queue queue, DirectExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(routingkey);
}
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jsonMessageConverter());
return rabbitTemplate;
}
}
package com.techgeeknext.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.techgeeknext.model.Employee;
import com.techgeeknext.service.RabbitMQSender;
@RestController
@RequestMapping(value = "/techgeeknext")
public class RabbitMQWebController {
@Autowired
private RabbitMQSender rabbitMQSender;
@GetMapping(value = "/rabbitmq/receive/message")
public String consume(@RequestParam("empName") String empName,@RequestParam("empId") String empId) {
Employee emp=new Employee();
emp.setEmpId(empId);
emp.setEmpName(empName);
rabbitMQSender.send(emp);
return "Message has been sent to the RabbitMQ techgeeknext successfully";
}
}
package com.techgeeknext.service;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.techgeeknext.model.Employee;
@Service
public class RabbitMQSender {
@Autowired
private AmqpTemplate amqpTemplate;
@Value("${techgeeknext.rabbitmq.exchange}")
private String exchange;
@Value("${techgeeknext.rabbitmq.routingkey}")
private String routingkey;
public void send(Employee empName) {
amqpTemplate.convertAndSend(exchange, routingkey, empName);
System.out.println("Send msg = " + empName);
}
}
Download Source Code
The full source code for this article can be found on below.Download it here - Spring Boot RabbitMQ Example