Spring boot Rabbitmq:

Here is an integration of spring boot with rabbitmq. Here learn how to sending messages using the following setups. Here Spring boot rabbitmq configurated with the Maven build tool.

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; } }

Publisher.java

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); } }

Subscriber.java

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 receivedMessage(String msg) { System.out.println("Received Message: " + msg); } }

application.properties

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

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"; } }

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); } }

pom.xml

<?xml version="1.0"?> -<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"> <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>1.5.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>

Output:

Request: endpoint: http://localhost:8080/send?msg=hai candid method:get Response: 200 Successfully Msg Sent Console: *****hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Received Message: hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Send msg = hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid Received Message: hai candid

Download

Spring boot Rabbitmq – Download