Javarevisited
Published in

Javarevisited

Apache Camel

How To Create Re-usable Routes in Apache Camel?

Hello 🖐, My Self Rohan Ravindra Kadam, Full Stack Developer, India

Welcome, Developer Community !! In the article, we try to understand reusable routes using Apache Camel. Before directly jumping into the article lets us try to understand certain question or queries related to the topic. We may have questions like What is Apache Camel? Why used Apache Camel? How to Create Re-usable Routes in Apache Camel?

credit Rohan Ravindra Kadam How To create Re-usable Routes in Apache Camel?

Hey Community, let us, deep-dive, into our Questions and find the answer for them.

Hey DevCommunity do not forget to check out more content on Instagram at @rohankadam_codes

⚡ What is Apache Camel?

Apache Camel ™ is a versatile open-source integration framework based on known Enterprise Integration Patterns.

Camel empowers you to define routing and mediation rules in various domain-specific languages (DSL, such as Java, XML, Groovy, Kotlin, and YAML). This means you get smart completion of routing rules in your IDE, whether in a Java or XML editor.

Apache Camel uses URIs to work directly with any kind of transport or messaging models such as HTTP, ActiveMQ, JMS, JBI, SCA, MINA or CXF, as well as pluggable Components and Data Format options. Apache Camel is a small library with minimal dependencies for easy embedding in any Java application. Apache Camel lets you work with the same API regardless of which kind of transport is used — so learn the API once, and you can interact with all the Components provided out-of-box.

Apache Camel supports Bean Binding and seamless integration with popular frameworks such as CDI and Spring. Camel also has extensive support for unit testing your routes.

⚡ Why used Apache Camel?

Let us try to list down certain use cases for Apache Camel

  1. Provides Support to Enterprise Integration Patterns(EIP)
  2. Runs Everywhere:- Apache Camel is standalone and can be embedded as a library within Spring Boot, Quarkus, Application Servers, and in the clouds. Camel subprojects focus on making your work easy.
  3. Packed With Components:- Packed with several hundred components used to access databases, message queues, APIs or anything under the sun. Helping you integrate with everything.
  4. Support Over 50 Data Formats:- Camel supports around 50 data formats, allowing translation messages in multiple formats and with support from industry-standard formats from finance, telco, healthcare, and more.

⚡ How To Create Re-usable Routes in Apache Camel?

Hey 🖐Community, in the prior section of the article section we tried to cover the Why & What about the Apache Camel? In section, we will try to understand the Implementation of the same.

Before jumping directly into the implementation do not forget to check out How to write the first route using Apache Camel? from the below link

Step 1: How to create Project Using Spring Initializr?

Step 2: What are the dependencies to include in the project?

Credit: Rohan Ravindra Kadam Dependencies

Step 3: How to create the re-usable route in apache camel?

It is oftentimes required to execute identical processing steps within multiple routes. Apache Camel equips us with a mechanism to call routing steps in a shared route. We can then reuse that route in an equivalent fashion to a method in a normal Java program.

This recipe will show us a technique for developing our routes in such as way that similar routing tasks can be defined within a shared route called from other routes.

Lets Gets Started:-

Lets us first determine the routing logic that we would like to reuse. In our case, we are going to create a route name MessageRoute


import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class MessageRoute extends RouteBuilder {

@Override
public void configure() throws Exception {
from("direct:StartMessage")
.log("Received Message is ${body}")
.end();
}
}

Let's Create a Top-level route for used or shared route


import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class ReusableRoute extends RouteBuilder {

@Override
public void configure() throws Exception {
from("direct:ReusableRoute")
.routeId("direct:ReusableRoute")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(" Reusable Route ");
}
})
.to("direct:StartMessage")
.end();
}
}

The shared route can be regarded as a part of the top-level route. It works on the same Exchange object and partakes in any transactions that the top-level route has instigated.

Step 4: Testing our camel Routes?

To test our camel route will be using the controller endpoint

package com.example.apachecameltest;import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.ExchangeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controler {
@Autowired
private ProducerTemplate producerTemplate;
@Autowired
private CamelContext camelContext;
@GetMapping
public void function() {
final Exchange
requestExchange = ExchangeBuilder
.anExchange(camelContext)
.build();
producerTemplate
.send("direct:ReusableRoute",requestExchange);
}
}

Hey 🖐 To Understand more about ProducerTemplate & Camel Context Check out the below link

Step 5:Output of Camel Route?

direct:StartMessage  : Received Message from   Reusable Route

🖐Conclusion:-

In the article, we tried to create re-usable routes in apache camel. We also tried to understand a bit about the apache camel as well. Please do share and like 💕if you find the article useful. Follow me on medium Rohan Ravindra Kadam and on Twitter at rohankadam25 & Instagram

⚡Bibliography

Thank You, Viewers — Rohan Ravindra Kadam

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store