Search Tutorials

Spring Boot + RabbitMQ Hello World Example | JavaInUse

Have a look at our latest post -

Spring Boot Security Architecture Tutorial

Spring Boot + RabbitMQ Hello World Example

In this post we will integrate Spring Boot and RabbitMQ instance.
In this tutorial we will be sending a message to RabbitMQ. In next tutorial we will see how to consume a RabbitMQ message using Spring Boot.
In a previous post we had seen how to get RabbitMQ up and running. Also in this tutorial we make use of the direct exchange for publishing the message to RabbitMQ. In another tutorial we have explained the various exchange types and their implementation using Spring Boot.
Spring Boot RabbitMQ Tutorial

RabbitMQ - Table Of Contents

What is Messaging? Getting Started with RabbitMQ - Install and start RabbitMQ. Spring Boot + RabbitMQ Publish Message Example Spring Boot + RabbitMQ Tutorial - Configure Listeners to consume messages using MessageListenerContainer Spring Boot + RabbitMQ Consume Message Example using RabbitListener Spring Boot + RabbitMQ Tutorial - Implement Exchange Types Spring Boot + RabbitMQ Tutorial - Retry and Error Handling Example Spring Cloud Stream - RabbitMQ Publish Message Example Spring Cloud Stream - RabbitMQ Consume Message Example Pivotal Cloud Foundry Tutorial - Deploying Spring Boot + RabbitMQ Application to PCF


This tutorial is explained in the below Youtube Video.

Lets Begin-

The project will be as follows-
Spring Boot RabbitMQ Eclipse Setup
Define the pom.xml as follows- Add the spring-boot-starter-amqp dependency.
<project xmlns="" xmlns:xsi=""


		<relativePath />




Define the domain class Employee as follows-
package com.javainuse.model;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id", scope = Employee.class)
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 + "]";


Have a look at our latest post -

Spring Boot Security Architecture Tutorial

Next define the configuration as follows-
package com.javainuse.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.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class RabbitMQConfig {

	String queueName;

	String exchange;

	private String routingkey;

	Queue queue() {
		return new Queue(queueName, false);

	DirectExchange exchange() {
		return new DirectExchange(exchange);

	Binding binding(Queue queue, DirectExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with(routingkey);

	public MessageConverter jsonMessageConverter() {
		return new Jackson2JsonMessageConverter();

	public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
		final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
		return rabbitTemplate;


Have a look at our latest post -

Spring Boot Security Architecture Tutorial

Define the Controller to expose a GET Request API as follows-
package com.javainuse.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.javainuse.model.Employee;
import com.javainuse.service.RabbitMQSender;

@RequestMapping(value = "/javainuse-rabbitmq/")
public class RabbitMQWebController {

	RabbitMQSender rabbitMQSender;

	@GetMapping(value = "/producer")
	public String producer(@RequestParam("empName") String empName,@RequestParam("empId") String empId) {
	Employee emp=new Employee();

		return "Message sent to the RabbitMQ JavaInUse Successfully";


Define the RabbitMQSender class which sends the message to the RabbitMQ using AmqpTemplate. We use the exchange and the exchange key.
Exchanges are message routing agents, defined per virtual host within RabbitMQ. An exchange is responsible for the routing of the messages to the different queues. An exchange accepts messages from the producer application and routes them to message queues with help of header attributes, bindings, and routing keys.
We will use a direct exchange instead. The routing algorithm behind a direct exchange is simple - a message goes to the queues whose binding key exactly matches the routing key of the message.
package com.javainuse.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.javainuse.model.Employee;

public class RabbitMQSender {
	private AmqpTemplate rabbitTemplate;
	private String exchange;
	private String routingkey;	
	public void send(Employee company) {
		rabbitTemplate.convertAndSend(exchange, routingkey, company);
		System.out.println("Send msg = " + company);
Next define th following properties in
Finally Define the Spring Boot Class with @SpringBootApplication annotation
package com.javainuse;

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

public class SpringBootHelloWorldApplication {

	public static void main(String[] args) {
				new Object[] { SpringBootHelloWorldApplication.class }, args);

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. Hit the url as follows- http://localhost:8080/javainuse-rabbitmq/producer?empName=emp1&empId=emp001
Spring Boot RabbitMQ Example
This will trigger the message to be sent to the javainuse queue.
Next go to the RabbitMQ console-http://localhost:15672/
Spring Boot RabbitMQ Consoler
We can see in the Queues section, a queue name java gets created and it has one message.
Spring Boot RabbitMQ Queue

Download Source Code

Download it -
Spring Boot + RabbitMQ Hello World Example