DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Spring Boot Microservices + Apache Camel: A Hello World Example
  • Open Source Integration With Apache Camel and How Fuse IDE Can Help
  • Aggregating REST APIs Calls Using Apache Camel
  • Java Microservices [Tutorials and Articles]

Trending

  • The Emergence of Cloud-Native Integration Patterns in Modern Enterprises
  • Build a Digital Collectibles Portal Using Flow and Cadence (Part 1)
  • Development of Custom Web Applications Within SAP Business Technology Platform
  • Continuous Integration vs. Continuous Deployment
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Introduction to Apache Camel

Introduction to Apache Camel

This tutorial introduces Apache Camel's architecture and shows you how to implement some simple Camel integrations and tests.

Jitendra Bafna user avatar by
Jitendra Bafna
CORE ·
Jun. 27, 17 · Tutorial
Like (16)
Save
Tweet
Share
28.83K Views

Join the DZone community and get the full member experience.

Join For Free

1.0 Overview

Apache Camel is a versatile Java-based open source enterprise service bus and supports most of the Enterprise Integration Patterns (EIP). Camel is also known as a routing and mediation engine as it effectively routes data between endpoints, while taking heavy loads like transformation of data formats, endpoint connectivity, and much more. Apache Camel provides support for bean binding and seamless integration with popular frameworks such as CDI, Spring, Blueprint, and Guice. Camel empowers you to write rules and meditation in a variety of domain-specific languages (DSL) including Java-based Fluent API, Spring or Blueprint XML Configuration files, and a Scala DSL. Apache Camel uses URIs to work directly with any kind of Transport or messaging model such as HTTP, ActiveMQ, JMS, JBI, SCA, MINA, or CXF, as well as pluggable components and data format options.

2.0 Architecture

Camel Context provides the runtime system. Processors handles things in between endpoints like routing, transformation, validation, enrichment, etc. Endpoints connects several systems to be integrated and it acts as URI or URL in a web application or destination JMS queue. Apache Camel offers different DSLs to realize the integration problems.


Image title

3.0 Download and Extract Apache Camel

  • Download the Apache Camel (Camel 2.19.1) from this link.

  • Unzip the apache-camel-x.xx.x and it will unzip all the jar files required for implementing integration solutions with Apache Camel.

Image title

4.0 Create Java Project and Add Camel Dependencies

  • Make sure you have Eclipse installed in your system for implementing Camel integrations.

  • Create a new Java project by selecting File < New < Java Project.

Image title

  • Now you need to add a jar dependency to build the path the application requires to implement Camel projects. Right click on "application" and select Build Path < Configure Build Path.

Image title

  • This will open a new dialog window. Now select Libraries < Add External Jars. You need to add camel-core-2.19.1.jar from the path \apache-camel-2.19.1\lib. Click OK.

Image title

5.0 Implementing Simple Apache Camel Integrations

Here we will implement a simple Apache Camel integration which transfers a file from one location to another.

  • Create Java class in your project and name it SimpleFileRouter.

  • Import the Apache Camel dependency into your Java class.

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

5.1 Camel Context

The CamelContext is the runtime system of Apache Camel and connects its different concepts, such as routes, components, or endpoints. Below is the Java code for initializing the Camel Context.

package com.file.demo;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class SimpleFileRouter {
public static void main(String [] args) throws Exception
{
CamelContext camelContext=new DefaultCamelContext();
camelContext.addRoutes(new RouteBuilder(){

});
camelContext.start();
Thread.sleep(5000);
camelContext.stop();
}

}

5.2 Domain Specific Language (DSLs)

Apache Camel offers various DSLs, such as Java-based Fluent API, Spring, or Blueprint XML Configuration files, and a Scala DSL. Spring XML DSL is based on the Spring framework and uses XML configuration. Java DSL has the best IDE support.  Groovy and Scala DSLs are similar to the Java DSL; in addition, they offer the typical features of modern JVM languages, such as concise code or closures.

5.3 Routes

Routes are important when you're defining the Camel integration; it is defined as shown below.

package com.file.demo;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class SimpleFileRouter {
public static void main(String [] args) throws Exception
{
CamelContext camelContext=new DefaultCamelContext();
System.out.println("Apache Camel Context Intialize....");
try
{
camelContext.addRoutes(new RouteBuilder(){
@Override
public void configure() throws Exception
{
from("file:F:\\Apache_Camel_Test\\IN?noop=true")
.to("file:F:\\Apache_Camel_Test\\OUT");
}
});
System.out.println("File Moved To Output Directory....");
camelContext.start();
Thread.sleep(5000);
camelContext.stop();
}catch (Exception Ex) {
System.out.println(Ex.toString());
}
}
}

The DSLs are very easy to use. A file will be dropped to the input directory and it will be processed and sent to out directory. Routes have extends "RouteBuilder" and override "configure" method. The route itself begins with "from" endpoints and ends at one or more "to" endpoints.

6.0 Testing

For testing, drop the file to input directory, and run your application as a Java application. Once you run the application, the file will be moved to the output directory.

7.0 Conclusion

Apache Camel is a very powerful open source ESB and it supports most of Enterprise Integration Patterns. It is very easy to implement Camel integrations. 

Now you know the basic components of Apache Camel and how to implement the integration application.

Apache Camel Spring Framework Domain-Specific Language Enterprise integration Java (programming language) Open source application Enterprise service bus

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot Microservices + Apache Camel: A Hello World Example
  • Open Source Integration With Apache Camel and How Fuse IDE Can Help
  • Aggregating REST APIs Calls Using Apache Camel
  • Java Microservices [Tutorials and Articles]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: