Camel Lesson 4: Using maven to set up a Camel project

In this Camel tutorial we will learn how to set up a basic Camel project using a Maven archetype which is based on Spring XML configuration file.

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 a Spring based Camel project using a Maven Archetype:

mvn archetype:generate                    

  -DarchetypeGroupId=org.apache.camel.archetypes   

  -DarchetypeArtifactId=camel-archetype-spring    

  -DarchetypeVersion=1.5.0              

  -DgroupId=myGroupId                  

  -DartifactId=myArtifactId  

Here is camel-context.xml which contains the route definition:

<?xml version="1.0" encoding="UTF-8"?>
 

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       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">

  <camel:camelContext xmlns="http://camel.apache.org/schema/spring">
 
    <camel:route>
      <camel:from uri="file:src/data?noop=true"/>
      <camel:choice>
        <camel:when>
          <camel:xpath>/person/city = 'London'</camel:xpath>
          <camel:log message="UK message"/>
          <camel:to uri="file:target/messages/uk"/>
        </camel:when>
        <camel:otherwise>
          <camel:log message="Other message"/>
          <camel:to uri="file:target/messages/others"/>
        </camel:otherwise>
      </camel:choice>
    </camel:route>
  </camel:camelContext>

</beans>

What is doing this route is pretty simple: it takes (without removing them, as there is the noop=true option) as input XML files contained in the src/data folder.

These files are processed using an XPath expression which checks for the Path /person/city. If the path belongs to London, files are moved in the target/messages/uk folder. Otherwise files are moved in the target/messages/others folder.

Run the Maven project using:

mvn camel:run

Advertisement

Cookie Alert