JBoss Fuse - Apache Camel



In this chapter, we will discuss what Apache Camel is and how it effectively routes data between endpoints, along with a few examples.

What is Apache Camel?

Apache Camel is an open source integration framework which was started in early 2007.

It is an EIP (Enterprise Integration Pattern) based approach which provides several out of the box patterns implementations that can be used to solve enterprise integration problems. EIP are nothing but proven solutions to the well documented and recurring problems in enterprise integration.

Camel is also known as routing and mediation engine as it effectively routes data between endpoints, while taking heavy load like transformation of data formats, endpoint connectivity and many more.

Basic Example

The prerequisites to use Apache Camel are −

  • Java
  • Maven
  • Redhat JBoss Fuse 6.1-GA-379

Create basic skeleton of Application

mvn:archetype generate 
–DgroupId = com.tutorialpoint.app 
–DartifactId = camel-first-app 
–DarchetypeGroupId = org.apache.camel.archetypes
–DarchetypeArtifactId = camel-archetype-spring 
–DinteractiveMode = false -X

This should generate the following directory structure.

Directory Structure

This is a basic skeleton of our Camel application being generated.

Edit camel-context.xml

Edit camel-first-app → src → main → resources → META-INF\spring\camel-context.xml to match as below

<?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 file
         (leaving them in place - see the 'noop' flag) 
         then performs content based routing on the message using XPath -->
			
      <route>
         <from uri = "file:///d:/src/data?noop=false"/>
         <choice>
            <when>
               <xpath>/person/city = 'London'</xpath>
               <log message = "UK message"/>
               <to uri = "file:///d:/target/messages/uk"/>
            </when>
				
            <otherwise>
               <log message = "Other message"/>
               <to uri = "file:///d:/target/messages/others"/>
            </otherwise>
				
         </choice>
			
      </route>
   </camelContext>
</beans>

Edit pom.xml

Add the following code inside <plugins></plugins>

<plugin>
   <groupId>org.apache.felix</groupId>
   <artifactId>maven-bundle-plugin</artifactId>
   <version>2.3.4</version>
   <extensions>true</extensions>
	
   <configuration>
      <instructions>
         <Bundle-SymbolicName>
            ${project.artifactId}
         </Bundle-SymbolicName>
         <Import-Package>*</Import-Package>
      </instructions>
   </configuration>
	
</plugin>

Change packaging type from jar → bundle.

<packaging>bundle</packaging>

Build the project using the following command −

mvn clean install

Install Project into Fuse

Start Fuse using Fuse.bat/start.bat. If you start Fuse using start.bat, use client.bat to connect to Fuse. You should get the UI as shown in the following screenshot.

Install Project in Fuse

This is the CLI for accessing Karaf and Fuse commands.

install –s mvn:com.tutorialpoint.app/camel-firt-app/1.0-SNAPSHOT

Test if your Project is Running

Now your application should be installed in Fuse. Copy data directory inside camel-first-app and place it in D:/src/ and it should copy message having city = London into D:/target/merssages/uk.

Place the input file in D:/src/data

Input

Message1.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<person user = "james">
   <firstName>James</firstName>
   <lastName>Strachan</lastName>
   <city>London</city>
</person>

Message2.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<person user = "hiram">
   <firstName>Hiram</firstName>
   <lastName>Chirino</lastName>
   <city>Tampa</city>
</person>

Output

In D:/target/messages/uk

<?xml version = "1.0" encoding = "UTF-8"?>
<person user = "james">
   <firstName>James</firstName>
   <lastName>Strachan</lastName>
   <city>London</city>
</person>

In D:/target/messages/others

<?xml version = "1.0" encoding = "UTF-8"?>
<person user = "hiram">
   <firstName>Hiram</firstName>
   <lastName>Chirino</lastName>
   <city>Tampa</city>
</person>
Advertisements