Setting up a Camel project with Maven



In this Camel tutorial we will learn how to set up a basic Camel project using its Maven archetypes which can generate both Java DSL and Spring XML based projects.

There are a few different approaches to configuring components and endpoints.

1. You can explicitly configure a Component using Java code as shown in this example:

context.addRoutes(new RouteBuilder() {
    public void configure() {
        from("test-jms:queue:test.queue").to("file://test");
    }
});

2. You can configure your Component or Endpoint instances in your Spring XML. When using Spring the CamelContext can be pre configured based on defined beans in spring XML.

The advantage of using Spring based Camel project is that you can build complex Camel routes declaratively, that is just by filling up the Camel Spring configuration file, named camel-context.xml

We will show here how to build example projects using Java DSL or Spring XML and Maven.

Creating a Camel Java DSL Project using Maven

You can create your Camel project as follows from the Command Line:

mvn archetype:generate \
 -DarchetypeGroupId=org.apache.camel.archetypes \
 -DarchetypeArtifactId=camel-archetype-java \
 -DarchetypeVersion=3.0.0 \
 -DgroupId=com.mastertheboss.camel \
 -DartifactId=camel-helloworld \
 -Dversion=1.0-SNAPSHOT

The project camel-helloworld will be created. Now import it in your favorite IDE. In our case we import it into IntelliJ Idea Community edition.

We will just edit the MainApp class to remove deprecated code and use Camel Main class instead. Here is how it should be like:

package com.mastertheboss.camel;

import org.apache.camel.main.Main;

public class MainApp {
    
    public static void main(String... args) throws Exception {
        // use Camels Main class
        Main main = new Main();
        main.addRouteBuilder(MyRouteBuilder.class);
        main.run(args);

    }

}

The Class MyRouteBuilder which has been created for you is a Java DSL class to route messages.

package com.mastertheboss.camel;

import org.apache.camel.builder.RouteBuilder;
 
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");
    }

}

As you can see, all this Class does is to process the input files (leaving them in place, as configured from the 'noop' flag) and then perform Content Based routing using an XPath expression.

The XML files are picked up from the folder 'data' which is included in your project.

Finally, within the file log4j2.properties you can set custom definitions for your Loggers:

appender.out.type = Console
appender.out.name = out
appender.out.layout.type = PatternLayout
appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n
rootLogger.level = INFO
rootLogger.appenderRef.out.ref = out

The project file (pom.xml) includes the following dependencies in it:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mastertheboss.camel</groupId>
  <artifactId>camel-helloworld</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>A Camel Route</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <dependencyManagement>
    <dependencies>
      <!-- Camel BOM -->
      <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-parent</artifactId>
        <version>3.0.0</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>

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

    <!-- logging -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-slf4j-impl</artifactId>
      <scope>runtime</scope>
    </dependency>

    <!-- testing -->
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <defaultGoal>install</defaultGoal>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>

      <!-- Allows the example to be run via 'mvn compile exec:java' -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <configuration>
          <mainClass>com.mastertheboss.camel.MainApp</mainClass>
          <includePluginDependencies>false</includePluginDependencies>
        </configuration>
      </plugin>

    </plugins>
  </build>

</project>

Run the Main class and check from the logs that the files have been routed:

[1) thread #2 - file://src/data] route1                         INFO  UK message
[1) thread #2 - file://src/data] route1                         INFO  Other message

Congratulations! you have just run your first Camel project with Maven

Source code for Camel Hello world project: https://github.com/fmarchioni/masteringintegration/tree/master/camel-helloworld

Creating a Camel Spring Project using Maven

If you want to create your Spring version of the same example you can use the maven archetype:

mvn archetype:generate                   \
  -DarchetypeGroupId=org.apache.camel.archetypes  \
  -DarchetypeArtifactId=camel-archetype-spring   \
  -DarchetypeVersion=3.0.0 \
  -DgroupId=com.mastertheboss.camel \
  -DartifactId=camel-helloworld-spring \
  -Dversion=1.0-SNAPSHOT

The main difference is that the project will not include Java resources but the following camel-context.xml file with the definition of the Camel Context and the route:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Configures the Camel Context-->

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <!-- here is a sample which processes the input files
         (leaving them in place - see the 'noop' flag)
         then performs content based routing on the message using XPath -->
    <route>
      <from uri="file:src/data?noop=true"/>
      <choice>
        <when>
          <xpath>/person/city = 'London'</xpath>
          <log message="UK message"/>
          <to uri="file:target/messages/uk"/>
        </when>
        <otherwise>
          <log message="Other message"/>
          <to uri="file:target/messages/others"/>
        </otherwise>
      </choice>
    </route>
  </camelContext>

</beans>

Here is the project tree:

src
├── data
│   ├── message1.xml
│   └── message2.xml
├── main
│   ├── java
│   │   └── com
│   │       └── mastertheboss
│   │           └── camel
│   └── resources
│       ├── log4j2.properties
│       └── META-INF
│           └── spring
│               └── camel-context.xml
└── test
    ├── java
    │   └── com
    │       └── mastertheboss
    │           └── camel
    └── resources

You can run the project with:

mvn install camel:run

Source code for the Camel Hello World Spring: https://github.com/fmarchioni/masteringintegration/tree/master/camel-helloworld-spring

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials
Cookie Policy

Cookie Policy This website uses cookies that are necessary to its functioning and required to achieve the purposes illustrated in the privacy policy. By accepting this OR scrolling this page OR continuing to browse, you agree to our privacy policy.

© 2020 Your Company. All Rights Reserved. Designed By JoomShaper

Please publish modules in offcanvas position.