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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Aggregating REST APIs Calls Using Apache Camel
  • Spring Boot Microservices + Apache Camel: A Hello World Example
  • A Systematic Approach for Java Software Upgrades
  • Building a Simple RAG Application With Java and Quarkus

Trending

  • How to Perform Custom Error Handling With ANTLR
  • Memory Leak Due to Time-Taking finalize() Method
  • Infrastructure as Code (IaC) Beyond the Basics
  • Agile’s Quarter-Century Crisis
  1. DZone
  2. Coding
  3. Frameworks
  4. Getting Started With Apache Camel Part 1

Getting Started With Apache Camel Part 1

By 
Krzysztof Kaczmarczyk user avatar
Krzysztof Kaczmarczyk
·
Jul. 02, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
10.7K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

Apache Camel is an open-source software integration framework implementing the patterns described in the Enterprise Integration Patterns book.

The detailed description of the framework can be found on its official website.

In this tutorial, we will have a look how to start a basic Camel project and what tools are available.

Creating a Basic Apache Camel Project

There are multiple ways of starting a new project using Apache Camel. We will first start with the simplest possible empty Java project and add Camel as a dependency. To do that, first we need to execute the following command:

Shell
xxxxxxxxxx
1
 
1
mvn archetype:generate -DgroupId=org.camel.mypackage -DartifactId=CamelApp -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false


The above command will generate an empty Java application using the maven-archetype-quickstart  Maven archetype.

The first thing we need to do is adding the Apache Camel library to the pom.xml dependencies file. It should look as follows:

XML
xxxxxxxxxx
1
 
1
<dependency>
2
  <groupId>org.apache.camel</groupId>
3
  <artifactId>camel-core</artifactId>
4
  <version>${camel.version}</version>
5
</dependency>


I will also require a camel.version variable to be defined in the properties section of the pom.xml file, like so:

XML
xxxxxxxxxx
1
 
1
<camel.version>3.1.0</camel.version>


Altogether, the file should look like the following:

XML
xxxxxxxxxx
1
83
 
1
<?xml version="1.0" encoding="UTF-8"?>
2
3
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
  <modelVersion>4.0.0</modelVersion>
6
7
  <groupId>org.camel.mypackage</groupId>
8
  <artifactId>CamelApp</artifactId>
9
  <version>1.0-SNAPSHOT</version>
10
11
  <name>CamelApp</name>
12
  <!-- FIXME change it to the project's website -->
13
  <url>http://www.example.com</url>
14
15
  <properties>
16
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17
    <maven.compiler.source>1.7</maven.compiler.source>
18
    <maven.compiler.target>1.7</maven.compiler.target>
19
    <camel.version>3.1.0</camel.version>
20
  </properties>
21
22
  <dependencies>
23
    <dependency>
24
      <groupId>org.apache.camel</groupId>
25
      <artifactId>camel-core</artifactId>
26
      <version>${camel.version}</version>
27
    </dependency>
28
    
29
    <dependency>
30
      <groupId>junit</groupId>
31
      <artifactId>junit</artifactId>
32
      <version>4.11</version>
33
      <scope>test</scope>
34
    </dependency>
35
  </dependencies>
36
37
  <build>
38
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
39
      <plugins>
40
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
41
        <plugin>
42
          <artifactId>maven-clean-plugin</artifactId>
43
          <version>3.1.0</version>
44
        </plugin>
45
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
46
        <plugin>
47
          <artifactId>maven-resources-plugin</artifactId>
48
          <version>3.0.2</version>
49
        </plugin>
50
        <plugin>
51
          <artifactId>maven-compiler-plugin</artifactId>
52
          <version>3.8.0</version>
53
        </plugin>
54
        <plugin>
55
          <artifactId>maven-surefire-plugin</artifactId>
56
          <version>2.22.1</version>
57
        </plugin>
58
        <plugin>
59
          <artifactId>maven-jar-plugin</artifactId>
60
          <version>3.0.2</version>
61
        </plugin>
62
        <plugin>
63
          <artifactId>maven-install-plugin</artifactId>
64
          <version>2.5.2</version>
65
        </plugin>
66
        <plugin>
67
          <artifactId>maven-deploy-plugin</artifactId>
68
          <version>2.8.2</version>
69
        </plugin>
70
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
71
        <plugin>
72
          <artifactId>maven-site-plugin</artifactId>
73
          <version>3.7.1</version>
74
        </plugin>
75
        <plugin>
76
          <artifactId>maven-project-info-reports-plugin</artifactId>
77
          <version>3.0.0</version>
78
        </plugin>
79
      </plugins>
80
    </pluginManagement>
81
  </build>
82
</project>
83


At this point, the directory structure should look like this:

Shell
xxxxxxxxxx
1
17
 
1
CamelApp
2
├── pom.xml
3
└── src
4
    ├── main
5
    │   └── java
6
    │       └── org
7
    │           └── camel
8
    │               └── mypackage
9
    │                   └── App.java
10
    └── test
11
        └── java
12
            └── org
13
                └── camel
14
                    └── mypackage
15
                        └── AppTest.java
16
17
11 directories, 3 files


Creating Camel Context

Camel context provides the runtime environment for the whole Apache Camel application. Usually, we will create one context per application and all Camel routes will be executed within the context.

We will set up a simple application where the context will be started when we run the application. After that we will let the context run for couple of seconds and then we will shut it down - this is not what you would do normally in the production environment, but we can have it as such to be able to play around with it.

We will open the App.jave file and modify it to look as follows:

Java
xxxxxxxxxx
1
31
 
1
package org.camel.mypackage;
2
3
import org.apache.camel.CamelContext;
4
import org.apache.camel.impl.DefaultCamelContext;
5
import org.apache.camel.builder.RouteBuilder;
6
7
public class App {
8
  public static void main( String[] args ) throws Exception {
9
    CamelContext context = new DefaultCamelContext();
10
11
    context.addRoutes(new RouteBuilder() {
12
      @Override
13
      public void configure() {
14
        from("file://./?fileName=in.csv&noop=true")
15
          .to("file://./?fileName=out.csv")
16
          .end();
17
        }
18
    });
19
20
    context.start();
21
22
    System.out.println("Camel context started. Wating 2s...");
23
24
    Thread.sleep(2000);
25
26
    System.out.println("Done Wating. Exiting.");
27
28
    context.stop();
29
  }
30
}
31


Most notable thing happening above are:

1. Importing packages which will be used

Java
xxxxxxxxxx
1
 
1
import org.apache.camel.CamelContext;
2
import org.apache.camel.impl.DefaultCamelContext;
3
import org.apache.camel.builder.RouteBuilder;


2. Creating a default Camel context:

Java
xxxxxxxxxx
1
 
1
CamelContext context = new DefaultCamelContext();


3. Adding a route to the context:

Java
xxxxxxxxxx
1
 
1
context.addRoutes(new RouteBuilder() {
2
  @Override
3
  public void configure() {
4
    from("file://./?fileName=in.csv&noop=true")
5
      .to("file://./?fileName=out.csv")
6
      .end();
7
    }
8
});


4. Starting and stopping the context:

Java
xxxxxxxxxx
1
 
1
context.start();
2
3
System.out.println("Camel context started. Wating 2s...");
4
5
Thread.sleep(2000);
6
7
System.out.println("Done Wating. Exiting.");
8
9
context.stop();


Camel context implements a Java Service lifecycle interface so it has  start()  and  stop()  methods. In addition to that, it implements  suspend()  and  resume()  methods so it is convenient and easy to manage the context lifecycle. From the Apache Camel official website:

The operations is paired: start/stop and suspend/resume.

Stop is performing a Graceful shutdown which means all its internal state, cache, etc is cleared. And the routes is being stopped in a graceful manner to ensure messages are given time to complete. If you start a CamelContext after a stop, then its performing a cold start, recreating all the state, cache etc. again.

Also, worth mentioning is the route itself. In the example above, we have created a very simple route which will take the contents of the  in.csv  file from the current directory and copy all the contents for the  out.csv  file in the same directory. The  noop=true  parameter tells Camel not to do anything with the file. Without this option Camel would move the the file into  .camel  directory after processing it.

We can now build, and run the application to see it working. First, we need to create some dummy data and put it in the  in.csv  file (the content of the file does not matter at this moment).

When that's done we can execute the application with the following Maven command:

Shell
xxxxxxxxxx
1
 
1
mvn clean install compile exec:java -Dexec.mainClass="org.camel.mypackage.App"


We should see the standard maven output followed by the following:

Shell
x
 
1
Camel context started. Wating 2s...
2
Done Wating. Exiting.


Also if we list the current directory now we should see a new file called out.csv  with the same contents that were put inside the  in.csv  file.

Summary

In the article we explored how to build and run a simple, basic application using Apache Camel.

There are multiple other ways to start a Camel application from scratch (eg. using the Maven Camel archetype or Camel Spring Boot archetype) but the purpose of this article was to show how to build Camel application from ground up without using automatically generated code so that it is more understandable how are all the components wired together and how the Camel application works.

Apache Camel application Java (programming language)

Published at DZone with permission of Krzysztof Kaczmarczyk. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Aggregating REST APIs Calls Using Apache Camel
  • Spring Boot Microservices + Apache Camel: A Hello World Example
  • A Systematic Approach for Java Software Upgrades
  • Building a Simple RAG Application With Java and Quarkus

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!