Getting started with Camel 3

Let’s have a look at what’s new in Camel 3 with an example project which shows how to run a Camel 3 standalone via the built-in Main class. Then, we will learn how to bootstrap a Camel application using the CamelContext.

Apache Camel 3 is a new family of products which include:

  • Camel 3: The core integration framework
  • Camel K: A lightweight Serverless Integration Platform Camel on Kubernetes & Knative
  • Camel Quarkus: Camel extensions for Quarkus Optimized JVM & Native compiled Java (GraalVM)

camel-core:

<dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-core</artifactId>
</dependency>

This contains all camel core dependencies (33 jars) and it’s what you will probably need when migrating from Camel 2.x

camel-core-engine:

<dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-core-engine</artifactId>
</dependency>

This contains only the core camel dependencies (12 jars).

Creating a sample Camel Java project.

To boostrap a Camel project we recommend using a Maven archetype. There are several available archetype, the most popular choices include:

  • camel-archetype-java – This archetype is used to create a new Maven project for Camel routes using Java DSL.
  • camel-archetype-main – This archetype is used to create a new Maven project for Camel routes running Camel standalone (camel-main).
  • camel-archetype-spring – This archetype is used to create a new Maven project for XML DSL routes using Spring.
  • camel-archetype-spring-boot – This archetype is used to create a new Maven project for Camel routes using Spring Boot.

In this tutorial we will be using the camel-archetype-java to generate a sample project:

mvn archetype:generate \ 
-DarchetypeGroupId=org.apache.camel.archetypes \ 
-DarchetypeArtifactId=camel-archetype-java \ 
-DarchetypeVersion=3.15.0 \ 
-DgroupId=com.masterspringboot.camel \ 
-DartifactId=camel-demo \ 
-Dversion=1.0-SNAPSHOT

The project will be created with some sample classes. The first one is the Camel Main Class:

public class MainApp {

    public static void main(String... args) throws Exception {
        Main main = new Main();
        main.configure().addRoutesBuilder(new MyRouteBuilder());
        main.run(args);
    }

}

The Class org.apache.camel.main.Main allows to bootstrap the Camel Context within a standard Java standalone application. Within the Main class you can use Java DSL to add custom configuration and Routes. In our example, we will add the MyRouteBuilder to start a Route:

The Class MyRoute processes a set of input files (leaving them in place – see the ‘noop’ flag). Then performs content based routing on the message using XPath

public class MyRouteBuilder extends RouteBuilder {

    public void configure() {

        from("file:src/data?noop=true")
            .choice()
                .when(xpath("/person/city = 'London'"))
                    .log("UK message")
                    .to("file:target/messages/uk")
                .otherwise()
                    .log("Other message")
                    .to("file:target/messages/others");
    }

}

You can run the Camel Main class with:

$ mvn install camel:run

Here’s the output:

Bootstrapping the Camel Context

Besides the Camel Main class, you can explicitly bootstrap the CamelContext to create new Camel Routes. In the following Class, we are creating a new Camel Context, then we add a Route to it. Finally, we call the non-blocking start method on it:

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

public final class CamelBasic {

    public static void main(String[] args) throws Exception {
        // create a CamelContext
        try (CamelContext camel = new DefaultCamelContext()) {

            // add routes which can be inlined as anonymous inner class
            // (to keep all code in a single java file for this basic example)
            camel.addRoutes(createBasicRoute());

            // start is not blocking
            camel.start();

            // so run for 10 seconds
            Thread.sleep(10_000);
        }
    }

    static RouteBuilder createBasicRoute() {
        return new RouteBuilder() {
            @Override
            public void configure() {
                from("timer:foo")
                        .log("Hello Camel");
            }
        };
    }
}

To run the above example, all you need is the Camel BOM and the camel-core dependency:

 <dependencyManagement>
        <dependencies>
            <!-- Add Camel BOM -->
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-bom</artifactId>
                <version>${camel.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

<dependencies>

  <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
  </dependency>
  
</dependencies>	

To keep learning Camel, we recommend checking Camel with Spring Boot example