A Brief introduction to Apache Camel

Reading Time: 4 minutes

Introduction

An “Integration Framework is something which is simple and allows for manageable abstractions of the complex system we are trying to integrate. Apache Camel is that “Integration Framework”. In all its entirety, Apache Camel is just a “routing engine” or to put it more accurately a “routing-engine builder”. Apache Camel allows use to define custom routing rules, allows us to decide from which source we want to accept messages. It also helps us to determine how to process those messages and then send those messages to various destinations. One of the most important “fundamental principles” of Apache Camel is that: It never makes assumptions on the type of data that it needs to process. Camel provides high-level abstractions that allow us to connect with a variety of systems using the same API, independent of protocol or data format. This eliminates the need for unnecessary conversions from one protocol to another.

Reasons to use Camel

Below are few of the many advantages that Apache Camel provides us with:

Routing and mediation engine

One of the core features of Camel is to provide a routing engine for messages. It enables the messages to move around based on the configuration. In Camel, these routes are configured using “enterprise integration patterns” and “domain specific language”.

Extensive component library

More than 280 components are available in Camel’s library. Camel can connect through transports, leverage APIs, and comprehend data types thanks to these components.

Enterprise integration patterns

Enterprise integration patterns, or EIPs, are useful not only because they provide a tried-and-true solution to an issue, but also because they assist in defining and communicating the problem. Camel is very heavily based on EIPS.

Domain-specific language

The DSL allows the developer to concentrate on the integration challenge rather than the tool—the programming language.

Payload-agnostic router

Camel can route any type of payload; you are not limited to transporting payloads in a standardised format, such as XML.

Easy Configuration

When feasible, the convention over configuration paradigm is used, which reduces configuration needs. Camel employs a simple and clear URI setup to configure endpoints directly in routes.

Now, Lets see these advantages in examples by trying to solve a very simple case. Our Use Case could be to copy a file from the source directory and move it to our destination. We will solve this use-case by using plain JAVA and then using Apache Camel.

Using Plain JAVA

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CopyFileUsingJava {

public static void main(String args[]) throws Exception {

    File source = new File("FileSource/sourceFile");
    File destination = new File("FileSource/destinationFile");
    destination.mkdir();
    File[] getfiles = source.listFiles());
    for (File sourcePath: getfiles) {
         if (sourcePath.isFile()) {
             File destPath = new File(
                 destination.getPath() +
                 File.separator +
                 sourcePath.getName());
             FileCopyUsingJava(sourcePath, destPath);
         }
    }
}
    private static void FileCopyUsingJava(File source, File dest) throws IOException {
        OutputStream getOutputStream = new FileOutputStream(dest);
        byte[] buffer = new byte[(int) source.length(); 
        FileInputStream getFileInputStream = new FileInputStream(source); 
        getFileInputStream.read(buffer);
            try{
                getOutputStream.write(buffer);
                }
            finally{
                getOutputStream.close(); 
                getFileInputStream .close();
            }
    }
}
   

Using Apache Camel

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org. apache.camel.impl.DefaultCamelContext;

public class CopyFileUsingApacheCamel {

    public static void main (String args []) throws Exception {
    CamelContext getContext = new DefaultCamelContext ();
    getContext.addRoutes (new RouteBuilder ( {
        public void configure () {
            from ("file:FileSource/sourceFile?noop=true")
            .to ("file:FileSource/destinationFile");
        });
    getContext.start();
    Thread.sleep(10000);
    getContext.stop();
    }
}

Most of the code that we see above is a boilerplate code and every Camel application makes use of a CamelContext, thats subsequently started and then stopped. We also add a sleep() method which allows Camel time to copy the file from source to destination.

We define Routes in Camel in such a way that flow when read. The route in the above example is: consume message “from source”, perform no-operation on it “noop=true” and then save it in the destination folder.

Apache Camel Architecture

Apache Camels architecture consists of : A “Routing Engine“, “Processor” and “Components“.

Routing Engine

An endpoint interface is provided by the routing engine to connect to external systems.

Processor

The processors module is responsible for message manipulation and mediation between endpoints. This module implements the EIPs that I discussed earlier.

Components

Apache Camel comes with a number of pre-built components. Use them to communicate with third-party systems. Camel-Kafka, for example, is used to communicate with Apache Kafka message brokers.

Conclusion

Apache Camel is a lightweight integration tool that doesn’t require a separate machine to use your integration system, and the language is relatively simple to learn and Furthermore, because it is an open source project, anyone can contribute to it, and we can reference the official documentation in the Apache repository.

Reference links : http://camel.apache.org

Stay tuned for more blogs at : https://blog.knoldus.com/

Leave a Reply