Search Tutorials


Spring Boot + Apache Camel + RabbitMQ - Hello World Example | JavaInUse

Spring Boot + Apache Camel + RabbitMQ - Hello World Example

In previous tutorials we had implemented examples using Spring Boot +RabbitMQ. In this tutorial we will be implementing an example using Spring Boot + Apache Camel + RabbitMQ. We have already implemented quite a few Apache Camel Examples before. Why use Apache Camel? In an enterprise, a number of systems of different types exist. Some of these may be legacy systems while some may be new. These systems often interact with each other,and need to be integrated. This interaction or integration is not easy as the implementations of the systems, their message formats may differ. One way to achieve this is to implement code which bridges these differences. However this will be point to point integration.
Enterprise Tight Coupling
If tomorrow again if there is change in a system the other might also have to be changed which is not good. Instead of this point to point integration which causes tight coupling we can implement an additional layer to mediate the differences between the systems. This results in loose coupling and not affect much our existing systems. Apache Camel is a rule-based routing and mediation engine that provides a Java object-based implementation of the Enterprise Integration Patterns using an API (or declarative Java Domain Specific Language) to configure routing and mediation rules.
Enterprise Mediation Loose Coupling

Apache Camel - Table of Contents

File Transfer Using Java DSL Apache Camel Apache Camel Java DSL + Spring Integration Hello World Example Apache Camel Exception Handling Using Simple Example Apache Camel Redelivery policy using example Integrate Apache Camel and ActiveMQ EIP patterns using Apache Camel Apache Camel Tutorial- Integrate Spring Boot+ Apache Camel Apache Camel Tutorial- Integrate with MySQL DB using SQL query Apache Camel EIP - Splitter and Aggregator pattern Apache Camel Unit Testing Apache Camel + Spring + Quartz Hello World Example Camel application deployment on JBoss Fuse Apache Camel + Apache CXF SOAP Webservices Apache Camel + JAX-RS REST Webservice Apache Camel + CXFRS REST Webservice Apache Camel Routing Slip EIP Pattern Apache Camel Dynamic Router Pattern Apache Camel Load Balancer EIP Pattern Apache Camel Interceptors Apache Camel + Kafka Hello World Example Apache Camel - Marshalling/Unmarshalling XML/JSON Data Example Calling and Consuming Webservices using Apache Camel Apache Camel Tutorial - Send SMTP Email Using Gmail Apache Camel Tutorial - SEDA component Hello World Example Apache Camel Tutorial - Idempotent Consumer using MemoryIdempotentRepository and FileIdempotentRepository Spring Boot + Apache Camel + RabbitMQ - Hello World Example Spring Boot + Apache Camel JDBC component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + Transaction Management Example

The project we will be developing is as follows
Apache Camel Spring Boot RabbitMQ Tutorial
The Maven Project is as follows-
Apache Camel Spring Boot RabbitMQ Maven
The pom.xml is as follows-
<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>
	<groupId>com.javainuse</groupId>
	<artifactId>camel-spring-boot</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath />
	</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.apache.camel</groupId>
			<artifactId>camel-spring-boot-starter</artifactId>
			<version>2.17.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.17.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-jackson</artifactId>
			<version>2.17.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-rabbitmq</artifactId>
			<version>2.17.0</version>
		</dependency>
	</dependencies>

</project>
Create the model class as follows-
package com.javainuse.model;

public class Employee {
	private int empId;
	private String name;
	private String designation;
	private double salary;

	public Employee() {
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesignation() {
		return designation;
	}

	public void setDesignation(String designation) {
		this.designation = designation;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public int getEmpId() {
		return empId;
	}

	public void setEmpId(int empId) {
		this.empId = empId;
	}

}
Define the camel route -
  • We will need to marshal the employee object to JSON. For this we will make use of Apache Camel JacksonDataFormat.
  • Create the camel route to marshal the employee object and then send it to the rabbitmq queue named javainuse. Also a rabbitmq exchange named javainuse is created
package com.javainuse.route;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.springframework.stereotype.Component;

import com.javainuse.model.Employee;

@Component
public class RabbitMQRoute extends RouteBuilder {

	@Override
	public void configure() throws Exception {

		JacksonDataFormat jsonDataFormat = new JacksonDataFormat(Employee.class);

		from("direct:startQueuePoint").id("idOfQueueHere").marshal(jsonDataFormat)
				.to("rabbitmq://localhost:5672/javainuse.exchange?queue=javainuse.queue&autoDelete=false").end();
	}
}
Create the controller-
  • Expose GET REST API to take employee parameters
  • Using the Camel ProducerTemplate to send employee object to RabbitMQ Queue.
package com.javainuse.controller;

import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.javainuse.model.Employee;

@RestController
public class SpringRabbitMQController {

	@Produce(uri = "direct:startRabbitMQPoint")
	private ProducerTemplate template;

	@RequestMapping(value = "/employee", method = RequestMethod.GET)
	public String createEmployee(@RequestParam int id, @RequestParam String name, @RequestParam String designation) {

		Employee emp = new Employee();
		emp.setName(name);
		emp.setDesignation(designation);
		emp.setEmpId(id);

		template.asyncSendBody(template.getDefaultEndpoint(), emp);
		return "";
	}
}
Finally create the bootstrap class using the SpringBootApplication annotation.
package com.javainuse;

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

@SpringBootApplication
public class SpringBootRabbitMQApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootRabbitMQApplication.class, args);
	}
}
In a previous tutorial we have shown how to install RabbitMQ and get started.
Start the Spring Boot Application.
  • Using the following url create an employee object -
    http://localhost:8080/employee?id=6&name=emp1&designation=manager
    Apache Camel Spring Boot RabbitMQ URL
  • Go to RabbitMQ console. We can see that an exchange named javainuse and a queue named javainuse is created and there is an employee message in it.
    Apache Camel Spring Boot RabbitMQ Console

Download Source Code

Download it -
Spring Boot + Apache Camel + RabbitMQ

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