Integrating Spring boot with RabbitMQ
RabbitMQ is a lightweight, reliable, scalable, and portable message broker based on the AMQP protocol.
Spring boot uses RabbitMQ to communicate through the AMQP protocol.
Spring Boot rabbitmq provides spring-boot-starter-amqp “Starter” for rabbitmq support
pom.xml
<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 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.spring-boot-rabbitmq</groupId> <artifactId>Spring-Boot-Rabbitmq</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Spring-Boot-Rabbitmq Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <finalName>Spring-Boot-Rabbitmq</finalName> </build> </project>
application.properties
Spring boot supports RabbitMQ configuration in application.properties or yaml file
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest jsa.rabbitmq.queue=jsa.queue1 jsa.rabbitmq.exchange=jsa.direct1 jsa.rabbitmq.routingkey=jsa.routingkey1
RabbitConfig.java
package com.candidjava.springboot.configuration; 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 RabbitConfig { @Value("${jsa.rabbitmq.queue}") private String queueName; @Value("${jsa.rabbitmq.exchange}") private String exchange; @Value("${jsa.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(); } @Bean public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) { final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); rabbitTemplate.setMessageConverter(jsonMessageConverter()); return rabbitTemplate; } }
HelloworldController.java
package com.candidjava.springboot.controller; 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.candidjava.springboot.publisher.Publisher; @RestController public class HelloworldController { @Autowired Publisher publisher; @RequestMapping("/send") public String sendMessage(@RequestParam("msg") String msg) { System.out.println("*****" + msg); for (int i = 0; i < 25; i++) { publisher.produceMsg(msg); } return "Successfully Msg Sent"; } }
RabbitMQ Publisher (Publisher.java)
Spring boot AmqpTemplate and AmqpAdmin are auto-configured, and you can autowire them directly into your own beans
package com.candidjava.springboot.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; @Value("${jsa.rabbitmq.routingkey}") private String routingKey; public void produceMsg(String msg) { amqpTemplate.convertAndSend(exchange, routingKey, msg); System.out.println("Send msg = " + msg); } public void publishEmail(String msg) { amqpTemplate.convertAndSend(exchange, routingKey, msg); System.out.println("Send msg = " + msg); } }
Rabbitmq listener or subscriber (Subcriber.java)
When the Rabbit infrastructure is present, any bean can be annotated with @RabbitListener to create a listener endpoint. If no RabbitListenerContainerFactory has been defined, a default SimpleRabbitListenerContainerFactory is automatically configured and you can switch to a direct container using the spring.rabbitmq.listener.type property.
package com.candidjava.springboot.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); } @RabbitListener(queues = "email") public void sendEMail(String msg) { System.out.println("Recieved Message: " + msg); } }
Application.java
package com.candidjava.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Download
Download source code from my github account Click here