Create a new Camel project

In this section, we'll show you Camel's Maven archetypes, which are preconfigured templates for creating various types of Camel projects. Archetypes in Maven define project templates and generate new projects based on those templates. They make creating new Maven-based projects easy because they create all the POM elements, as well as configuration. The table below shows the list of Maven archetype provided by Camel:

Archetype Name Description
camel-archetype-java Creates a Camel project that loads up a CamelContext in Spring and defines a sample route in Java.
camel-archetype-spring Creates a Camel project that loads up a CamelContext in Spring and defines a sample route in the Spring DSL
camel-archetype-activemq Creates a Camel project that has an embedded Apache ActiveMQ broker.
camel-archetype-component Creates a new Camel component.
camel-archetype-scala Creates a Camel project with a sample route in the Scala DSL.
camel-archetype-war Creates a Camel project that includes the Camel web console, REST API, and a few sample routes as a WAR file.

Camel's Java archetype

The Camel Java archetype allows you to create a Camel project that defines a Spring based context in a Spring configuration file. When the Context is loaded, the routes, defined in Java DSL will be started. The command below allows you to use the Camel Maven archetype. The project will be named order-router and the package name will be com.sungard.infinity.isb.

mvn archetype:create -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-java -DarchetypeVersion=2.5.0 -DgroupId=com.sungard.infinity.isb
-DartifactId=order-router

By following the steps below, you will be able to create a simple Camel route in your project. This route can be easily integrated within a standard web project.

Camel's Java archetype project structure

Route Developement

If you used camel-archetype-java to generate the project, the following file src /META-INF/spring/camel-context.xml is created by default. Please note that the name of the Spring file should use this pattern "*-context.xml" to be automatically loaded when Stardust starts. Otherwise, you need to edit your web.xml file. The contextConfigLocation parameter refers to your Camel Spring context file.

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>WEB-INF/config/ipp/spring/*-context.xml,classpath*:META-INF/spring/*-context.xml</param-value>
</context-param>

The created Java class extends org.apache.camel.builder.RouteBuilder and contains in its configure method a sample route which use the file component to receive files. The Figure below implements a typical EIP pattern, a content-based router. The sample reads input messages from a directory, applies an XPath predicate to each message's XML content, and, based on the result, chooses a different route for the output messages.

package com.sungard.infinity.isb;

public class SimpleRoute extends RouteBuilder {

    public void configure() throws Exception {
		
	  from ("file:src/data?noop=true").
            choice ().
                when (xpath("/person/city = 'London'")).to("file:target/messages/uk").
                otherwise().to("file:target/messages/others");    
	}
}

The generated Spring configuration file will scan the package com.sungard.infinity.isb. All classes inheriting from org.apache.camel.builder.RouteBuilder will be loaded.

<?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">

	<camelContext xmlns="http://camel.apache.org/schema/spring">
		<package>com.sungard.infinity.isb</package>
	</camelContext> 
</beans>

Camel's Spring archetype

The command below allows you to create a new Maven project for Camel. It Creates a Camel project that loads up a camelContext in Spring. By default, there is a simple route defined in the Spring DSL.

mvn archetype:generate                   
  -DarchetypeGroupId=org.apache.camel.archetypes  
  -DarchetypeArtifactId=camel-archetype-spring   
  -DarchetypeVersion=1.5.0             
  -DgroupId=myGroupId                  
  -DartifactId=myArtifactId   

The configuration file is located in src/main/resources/META-INF/spring/camel-context.xml.

To be able to import the project into Eclipse, you can use the Eclipse Plug-in to generate Eclipse IDE files (*.classpath, *.wtpmodules and the .settings folder).

cd myArtifactId
mvn eclipse:eclipse

The Figure below shows the directory structure created by Maven's archetype:

Camel's Spring archetype project structure

Route Developement

By following the steps below, you will be able to create a simple Camel route within your project. This route can be easily integrated within a standard web project.

When using camel-archetype-spring to generate the project, the following file src/META-INF/spring/camel-context.xml is created by default. You can now edit it according to your need and you can define your Camel routes

Please note that name of the Spring file should use this pattern "*-context.xml" to be automatically loaded when Stardust starts. Otherwise, you need to edit your web.xml file. The contextConfigLocation parameter refers to you Camel Spring context file.

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>WEB-INF/config/ipp/spring/*-context.xml,classpath*:META-INF/spring/*-context.xml</param-value>
</context-param>

The sample project contains no generated Java class. The definition of the routes is in the Spring configuration file. The Figure defines the same route as the route created with the camel-archetype-java.

<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
    <camel:package>${package}</camel:package>
    <!-- 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 -->
    <camel:route>
      <camel:from uri="file:src/data?noop=true"/>
      <camel:choice>
        <camel:when>
          <camel:xpath>/person/city = 'London'</camel:xpath>
          <camel:to uri="file:target/messages/uk"/>
        </camel:when>
        <camel:otherwise>
          <camel:to uri="file:target/messages/others"/>
        </camel:otherwise>
      </camel:choice>
    </camel:route>
  </camel:camelContext>

Running the project

The project can be run via the Camel Maven Plug-in as follows

cd myArtifactId
mvn camel:run

Another way to run the project is to use org.apache.camel.spring.Main as Main class. It's useful to debug your routes.

  • define a new run configuration into Eclipse (see Figure below: Define a run configuration in Eclipse)
  • set org.apache.camel.spring.Main as Main class
  • and specify -ac "/META-INF/spring/camel-context.xml" as program arguments
  • Define a run configuration in Eclipse

    Camel's Web archetype

    The camel-archetype-war archetype allows to create a Camel project that includes a web console. Using the console, you can start/stop a Camel route, edit a route,... The command below will create a project having order-router as a project name

    mvn archetype:create -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-war -DarchetypeVersion=2.5.0 -DgroupId=com.sungard.infinity.isb -DartifactId=order-router