Spring Boot + Apache Camel + Rabbitmq(2023) Example | CodeUsingJava






















Spring Boot + Apache Camel + Rabbitmq(2023) Example

An enterprise application consist of a number of systems. There comes the need to integrate all the systems. So there may be cases that the systems have different message formats. Here comes the need of Apache Camel.
Basic intro

By using the Apache Camel, it is focussed on easier integration with Rabbitmq and making more feasible for the developers.
Apache camel makes it more feasible by providing a good linking between the API.
Need of Apache camel

Here With the help of a sample example, integration of apache camel, rabbitmq is done using spring boot application.
  • 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 https://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.5.2</version>
    			<relativePath/> <!-- lookup parent from repository -->
    		</parent>
    		<groupId>com.codeusingjava</groupId>
    		<artifactId>ApacheWithRabbitMQ</artifactId>
    		<version>0.0.1-SNAPSHOT</version>
    		<name>ApacheCamelWithRabbitMQ</name>
    		<description>Spring boot Rabbitmq Error Handling</description>
    		<properties>
    			<java.version>1.8</java.version>
    		</properties>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-starter</artifactId>
    			</dependency> 
    			
    			<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>
    	
    			<dependency>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-starter-test</artifactId>
    				<scope>test</scope>
    			</dependency>
    		</dependencies>
    	
    		<build>
    			<plugins>
    				<plugin>
    					<groupId>org.springframework.boot</groupId>
    					<artifactId>spring-boot-maven-plugin</artifactId>
    				</plugin>
    			</plugins>
    		</build>
    	
    	</project>
    	
    
    Create the model class and define its properties. The model class i.e. Corporation class as follows-
    package com.codeusingjava.model;
    
    
    	public class Corporation {
    	  int noofemployee;
    	  String name;
    	  String location;
    	  
    	public Corporation() {
    		super();
    	
    	}
    	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 Routing class for the Rabbitmq Routing can be defined in the following manner-
    For the process of Rabbitmq Routing, there is a need to have a conversion of the model object into the JSON format so we will use JacksonDataFormat for the purpose of marshelling the java object.
    After creating the routing, we will send the object to the queue which is inside the Rabbitmq server.
     
    package com.codeusingjava.util;
    
    import org.apache.camel.builder.RouteBuilder;
    import org.apache.camel.component.jackson.JacksonDataFormat;
    import org.springframework.stereotype.Component;
    
    import com.codeusingjava.model.Corporation;
    
    @Component
    public class RabbitMQRouting extends RouteBuilder {
    
    	@Override
    	public void configure() throws Exception {
    
    		JacksonDataFormat jsonDataFormat = new JacksonDataFormat(Corporation.class);
    
    		from("direct:startQueuePoint").id("idOfQueueHere").marshal(jsonDataFormat)
    				.to("rabbitmq://localhost:5672/myqueue.exchange?queue=myqueue.queue&autoDelete=false").end();
    	}
    }
    
    This is the controller class where the url which is to be executed is defined:-
    ProducerTemplate is used for the purpose of sending the message instance in an exchange to the endpoint.
    It also provides a method named asyncSendBody by which it is possible to send the message body asynchronously.
    package com.codeusingjava.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.codeusingjava.model.Corporation;
    
    
    @RestController
    public class SpringRabbitMQController {
    
    	@Produce(uri = "direct:startRabbitMQPoint")
    	private ProducerTemplate template;
    
    	@RequestMapping(value = "/corporation", method = RequestMethod.GET)
    	public String create(@RequestParam String name, @RequestParam String location, @RequestParam int noofemployee) {
    
    		Corporation corporation=new Corporation();
    		corporation.setName(name);
    		corporation.setNoofemployee(noofemployee);
    		corporation.setLocation(location);
    
    
    		template.asyncSendBody(template.getDefaultEndpoint(), corporation);
    		return "Successfully Executed...";
    	}
    }
    
    The main class for the ApacheCamelWithRabbitMqApplication is as follows-
     
    package com.codeusingjava;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class ApacheCamelWithRabbitMqApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(ApacheCamelWithRabbitMqApplication.class, args);
    	}
    
    }
    
    
If we now run the application we get the output as follows-
We run the program as spring boot application and then execute the following url in our browser-
http://localhost:8080/corporation?name=abc&location=mumbai&noofemployee=5
Spring Boot api

Downloads-

Spring Boot + Apache Camel + RabbitMQ Example