The project we will be developing is as follows

The Maven Project is as follows-

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

-
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.

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