Spring Cloud Stream Tutorial - Publish Message To RabbitMQ Simple Example | JavaInUse

Spring Cloud Stream Tutorial - Publish Message to RabbitMQ Simple Example

In this tutorial, we understand what is Spring Cloud Stream and its various terms. We then implement a simple example to publish message to RabbitMQ messaging using Spring Cloud Stream.

Spring Cloud - Table Of Contents

Microservice Registration and Discovery with Spring cloud using Netflix Eureka- Part 1. Microservice Registration and Discovery with Spring cloud using Netflix Eureka - Part 2. Microservice Registration and Discovery with Spring cloud using Netflix Eureka - Part 3. Microservice Registration and Discovery with Spring cloud using Netflix Eureka - Part 4. Spring Cloud- Netflix Eureka + Ribbon Simple Example Spring Cloud- Netflix Eureka + Ribbon + Hystrix Fallback Simple Example Spring Cloud- Netflix Hystrix Circuit Breaker Simple Example Spring Cloud- Netflix Feign REST Client Simple Example Spring Cloud- Netflix Zuul +Eureka Simple Example Spring Cloud Config Server using Native Mode Simple Example Spring Cloud Config Server Using Git Simple Example Spring Boot Admin Simple Example Spring Cloud Stream Tutorial - Publish Message to RabbitMQ Simple Example Spring Cloud Stream Tutorial - Consume Message from RabbitMQ Simple Example Spring Cloud Tutorial - Publish Events Using Spring Cloud Bus Spring Cloud Tutorial - Stream Processing Using Spring Cloud Data Flow

What is Spring Cloud Stream ? Need for it.

In previous examples we had implemented examples for integrating Spring Boot Applications with Messaging Systems like Apache Kafka and RabbitMQ. If you look at these examples these required a lot of configuration code which was Broker specific. For example in case of RabbitMQ integration with Spring Boot we had to write code to create AmqpTemplate Template and Bindings. So if tomorrow the Messaging System changes we will also need to make application code changes.
Spring Cloud helps solve this problem using Spring Cloud Stream. Using Spring Cloud Stream we can develop applications where we do not need to specify the implementation details of the messaging system we want to use. We just need to specify the required binding dependencies and Spring Cloud Stream will the integrate the messaging systems to Spring Boot Application.
Spring Cloud Stream Tutorial
Spring Cloud Concepts-
  • Binder - Depending upon the messaging system we will have to specify a the messaging platform dependency, which in this case is RabbitMQ
  • <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency>
  • Source - When a message is needed to be published it is done using Source. The Source is an interface having a method annotated with @Output. The @Output annotation is used to identify output channels. The Source takes a POJO object, serializes it and then publishes it to the output channel.
    public interface EmployeeRegistrationSource {
        MessageChannel employeeRegistration();
  • Channel - A channel represents an input and output pipe between the Spring Cloud Stream Application and the Middleware Platform. A channel abstracts the queue that will either publish or consume the message. A channel is always associated with a queue. With this approach, we do not need to use the queue name in the application code. So if tomorrow the queue needs to be changed, we dont need to change the application code.
    For example in the EmployeeRegistrationSource we have specified the channel name as employeeRegistrationChannel. In application.properties we have associated this channel with a RabbitMQ Exchange.

Lets Begin-

We will be developing the project as follows -
Spring Cloud Stream Eclipse Tutorial
The Maven project will be as follows -
Spring Cloud Stream Eclipse Maven
The pom.xml will be as follows with the binder dependency of RabbitMQ as follows-
       <?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">


	<description>Demo project for Spring Cloud Stream publishing</description>

		<relativePath /> <!-- lookup parent from repository -->






Create the Spring Boot Bootstrap class with the SpringBootApplication annotation as follows-
   package com.javainuse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class EmployeeRegistrationApplication {

	public static void main(String[] args) {
		SpringApplication.run(EmployeeRegistrationApplication.class, args);
Next we define the model class Employee-
   package com.javainuse.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee {

	private String empName;
	private String empID;

	public String getEmpName() {
		return empName;

	public void setEmpName(String empName) {
		this.empName = empName;

	public String getEmpID() {
		return empID;

	public void setEmpID(String empID) {
		this.empID = empID;

	public String toString() {
		return "Employee [empName=" + empName + ", empID=" + empID + "]";

Next define the Source class. This will simply be an interface that defines ways of obtaining the MessageChannel object needed to send the message. Here we define the output channel named as employeeRegistrationChannel.
   package com.javainuse.source;

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

public interface EmployeeRegistrationSource {

	MessageChannel employeeRegistration();

Next we define a simple controller that will make use of the above defined classes to publish the message upon receiving the employeeRegistration request. Here the @EnableBinding annotation tells Spring Cloud Stream that you want to bind the Controller to a message broker.
   package com.javainuse.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.javainuse.model.Employee;
import com.javainuse.source.EmployeeRegistrationSource;

public class EmployeeRegistrationController {

	EmployeeRegistrationSource employeeRegistrationSource;

	public String orderFood(@RequestBody Employee employee) {
		return "Employee Registered";
Finally we specify the properties. Here we specify the RabbitMQ properties. Also we associate the channel to the queue to be used -

We are done with the required Java code. Now lets start RabbitMQ. As we had explained in detail in the Getting started with RabbitMQ perform the steps to start the RabbitMQ.
Next start the Spring Boot Application by running it as a Java Application. Create a POST request with payload as follows
Spring Cloud Stream Example
Send the above request, it trigger the message to be sent to the RabbitMQ.
Next go to the RabbitMQ console-http://localhost:15672/. We can see in the Exchange section, an exchange named employeeRegistration gets created and it has one message.
Spring Cloud Stream RabbitMQ Example
In the next tutorial we will see how to bind this exchange to a queue and consume this RabbitMQ message using Spring Cloud Stream.

Download Source Code

Download it -
Spring Cloud Stream - RabbitMQ Publish Message Example

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions