Spring Boot + RabbitMQ Cloud Publisher Example(2021) | CodeUsingJava






















Spring Boot + RabbitMQ Cloud Publisher Example(2021)

In this tutorial we will be implementing Rabbitmq Cloud Publisher with the help of a simple example.
Spring cloud Stream is a framework by which message-driven or event-driven microservices can be created. This microservice are then connected using a message-broker. Rabbitmq is a type of message-broker which helps to communicate between the microservices.It uses the concept of Advanced Message Queuing Protocol(AMQP).
Spring Boot introduction

The digrammatic representation between the message publisher and the Rabbitmq message broker is as follows-
Spring Boot Maven Project
In this example, we will make a Rabbitmq message publisher application which will send the message to Rabbitmq.
  • This will be the standard directory layout for maven project structure-
    Spring Boot Maven Project
    We need to start by creating a Maven pom.xml(Project Object Model) file. The pom.xml file contains the project configuration details.
    	<?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 http://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.3.RELEASE</version>
    			<relativePath/> <!-- lookup parent from repository -->
    		</parent>
    		<groupId>com.example</groupId>
    		<artifactId>PublisherService</artifactId>
    		<version>0.0.1-SNAPSHOT</version>
    		<name>PublisherService</name>
    		<description>Demo project for Spring Boot</description>
    	
    		<properties>
    			<java.version>1.8</java.version>
    			<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    		</properties>
    	
    		<dependencies>
    			<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>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-stream</artifactId>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
    			</dependency> 
    
    			<dependency>
    				<groupId>com.rabbitmq</groupId>
    				<artifactId>amqp-client</artifactId>
    				<version>5.6.0</version>
    			</dependency>
    			<dependency>
    			  <groupId>com.fasterxml.jackson.dataformat</groupId>
    			  <artifactId>jackson-dataformat-xml</artifactId>
    			  <version>2.9.0</version>
    			</dependency>
    	
    			<dependency>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-starter-test</artifactId>
    				<scope>test</scope>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-stream-test-support</artifactId>
    				<scope>test</scope>
    			</dependency>
    		</dependencies>
    	
    		<dependencyManagement>
    			<dependencies>
    				<dependency>
    					<groupId>org.springframework.cloud</groupId>
    					<artifactId>spring-cloud-dependencies</artifactId>
    					<version>0</version>
    					<type>pom</type>
    					<scope>import</scope>
    				</dependency>
    			</dependencies>
    		</dependencyManagement>
    	
    		<build>
    			<plugins>
    				<plugin>
    					<groupId>org.springframework.boot</groupId>
    					<artifactId>spring-boot-maven-plugin</artifactId>
    				</plugin>
    			</plugins>
    		</build>
    	
    	</project>
    	
    The application.properties file can be defined as follows-
    All the configurations related to RabbitMQ are done in this file.
    The name of the exchange defined is Corporatedestination, here the message queue will be bounded.
    server.port=8081
    spring.rabbitmq.host=localhost
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=guest
    spring.rabbitmq.password=guest
    spring.cloud.stream.bindings.output.destination=Corporatedestination
    
    logging.level.org.springframework.cloud.stream=debug
    
    	
    Creating the model class with all the getters, setters and the constructors defined in it.
    	package com.codeusingjava.model;
    
    
    	public class Corporation {
    	  int noofemployee;
    	  String name;
    	  String location;
    	  
    	public Corporation() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    	public Corporation(int noofemployee, String name, String location) {
    		super();
    		this.noofemployee = noofemployee;
    		this.name = name;
    		this.location = location;
    	}
    	public int getNoofemployee() {
    		return noofemployee;
    	}
    	public void setNoofemployee(int noofemployee) {
    		this.noofemployee = noofemployee;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getLocation() {
    		return location;
    	}
    	public void setLocation(String location) {
    		this.location = location;
    	}
    	  
    	  
    	 
    	}
    	
    The controller for sending the message can be defined in the following manner-
    The source class has a special method known as output method.This method of the source class helps in the configuration of output message signal to send the message to RabbitMQ server.
    The annotation @EnableBinding is used for the purpose of reading as well as publish the message to the RabbitMQ broker.
     
    package com.codeusingjava.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.stream.annotation.EnableBinding;
    import org.springframework.cloud.stream.messaging.Source;
    import org.springframework.integration.support.MessageBuilder;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.codeusingjava.model.Corporation;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    @RestController
    @EnableBinding(Source.class)
    public class MsgController {
    
    	@Autowired
    	Source source;
    	
    	@PostMapping(value="/getdetails")
    	public String sendMessage(@RequestBody String payload) throws JsonParseException, IOException {
    		ObjectMapper ob = new ObjectMapper();
    		Corporation corporation=null;
    		try
    		{
    			corporation= ob.readValue(payload, Corporation.class);
    		}
    		catch(JsonMappingException ex)
    		{
    			System.out.println(ex);
    		} 
    		catch(IOException ex)
    		{
    			System.out.println(ex);
    		}
            catch(Exception ex)
    		{
            	System.out.println(ex);
    		}
    		source.output().send(MessageBuilder.withPayload(corporation).setHeader("myheader", "myheaderValue").build());
    		System.out.println("success");
    		return "Message sent to RabbitMQ";
    	}
    
    }
    
    	
    The main class for the CloudRabbitMQPublisher application is defined in the following manner-
      
    package com.codeusingjava;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class MsgPublishApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(MsgPublishApplication.class, args);
    	}
    
    }
    
    	
Run the application
If we now run the application we get the output as follows-
We run the following url in insomnia client-
http://localhost:8081/getdetails
We need to specify the Header attribute as Content-type: application/json
Spring boot test api
Now if we open the Rabbitmq console by using the following url- http://localhost:15672, we get the output as follows-
Spring boot  rabbitmq console
When we click on the exchange, we see that the graph shows that it has received a message.
Spring boot console
We can see the logger message at sring boot console also-
Spring boot console

Downloads-

Spring Boot + RabbitMQRabbitmq Cloud Publisher Example