Apache ServiceMix (Fuse ESB) with Camel

Apache ServiceMix (Fuse ESB) with Camel

Today I will present a quick intro tutorial into ESB and Camel. While going through the examples on the ServiceMix website I have encountered a few problems preventing me from developing the simplest applications. As the ServiceMix tutorial is a bit outdated I present you with the similar corrected version of the original tutorial.

Download and install Fuse ESB – based on Apache ServiceMix

For the purpose of this tutorial we will use Fuse ESB version 4.4.1. On the FuseSource website you can find the version for Mac, Linux and Windows as well as build your own version from source.
When you finish downloading, unpack the zipped archive into a choosen directory (without spaces in its name), eg. ‘/home/kris/’ -> you should end up with ‘/home/kris/apache-servicemix-4.4.1-fuse-01-11’

Test your installation

You can test your installation by going to the ‘bin’ folder of your servicemix installation and executing: ‘./servicemix’ command.
You should see something similar to this:

You can shutdown you ServiceMix instance by typing ‘shutdown’ and then ‘yes’. For simple debugging purposes you can type ‘log:tail’ to see the constantly scrolling logs of what’s happening inside your ServiceMix instance.

Create a basic folder for your entire ServiceMix application

For all the modules we are going to create in this tutorial we need to create a parent folder first, choose any destination you like and name the folder whatever suits you, let’s say I will name mine ‘softwarepassion’.

Create pom.xml file for your parent folder

This step includes simple ‘copy and paste’ basic parent pom.xml file for our project, this file is identical to the one found on the official ServiceMix tutorial:

<?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.softwarepassion.tutorial.camel</groupId>
  <artifactId>parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>SMX-Camel :: Tutorial</name>
  <url>http://servicemix.apache.org</url>
</project>

Create camel based service unit

Navigate into our parent project directory (‘softwarepassion’) and execute the following maven command:

mvn archetype:create -DarchetypeArtifactId=servicemix-camel-service-unit \
-DarchetypeGroupId=org.apache.servicemix.tooling -DartifactId=tutorial-camel-su

This should create a new maven based project with its own pom.xml file. The command additionally updates our parent pom.xml file with info about new child project.

Create service assembly

This is the main module of our project. Service assembly gathers other service units for the project into a single deployable assembly.
To create our service assembly, go to the parent folder (‘softwarepassion’) and execute the following maven command:

mvn archetype:create -DarchetypeArtifactId=servicemix-service-assembly \
-DarchetypeGroupId=org.apache.servicemix.tooling -DartifactId=tutorial-camel-sa

The outcome would be similar to the previous step.
Once you create a service assembly project you would need to update service assembly project pom.xml file with the following dependency information:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>com.softwarepassion.tutorial.camel</groupId>
      <artifactId>tutorial-camel-su</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
  ...
</project>

Full pom.xml file of your service assembly should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>parent</artifactId>
    <groupId>com.softwarepassion.tutorial.camel</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.softwarepassion.tutorial.camel</groupId>
  <artifactId>tutorial-camel-sa</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jbi-service-assembly</packaging>
  <name>Apache ServiceMix :: Service Assembly</name>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.4.3</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.servicemix.tooling</groupId>
        <artifactId>jbi-maven-plugin</artifactId>
        <version>4.4</version>
        <extensions>true</extensions>
        <configuration>
          <type>service-assembly</type>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>com.softwarepassion.tutorial.camel</groupId>
      <artifactId>tutorial-camel-su</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

At this point we have the full project skeleton ready – parent project with a single service unit and service assembly.

Build and test deploy

Now we can execute a test build and deploy our application to Apache ServiceMix. Go to the parent directory and execute the following command:

mvn install

You should see a ‘BUILD SUCCESSFUL’ message:

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] ------------------------------------------------------------------------
[INFO] Softwarepassion-Camel :: Tutorial ..................... SUCCESS [1.156s]
[INFO] Apache ServiceMix :: Camel Service Unit ............... SUCCESS [4.367s]
[INFO] Apache ServiceMix :: Service Assembly ................. SUCCESS [0.998s]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14 seconds
[INFO] Finished at: Wed Dec 07 11:42:47 CET 2011
[INFO] Final Memory: 51M/366M

Copy the service assembly zip file into ‘deploy’ directory of you ServiceMix installation (make sure its running first).

For my setup it looks like this:

cp tutorial-camel-sa/target/tutorial-camel-sa-1.0-SNAPSHOT.zip /home/kris/apache-servicemix-4.4.1-fuse-01-11/deploy/

Congratulations, you have reached the point where you have basic ESB application with Camel routing set-up up and running.

Modify camel-context.xml file

For the purposes of this tutorial we will create a simple Camel route using Java DSL. First you need to modify camel-context.xml file to inform that we are going to use java dsl style:
You can find camel-context.xml file inside service unit project ‘tutorial-camel-su/src/main/resources’

<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd
      http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

        <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
                <package>com.softwarepassion.tutorial.camel</package>
        </camelContext>
</beans>

Implement Camel route

Now we are going to the real job. We need to add implementation for the ‘configure(..)’ method of our RouteBuilder:
Replace the class ‘MyRouteBuilder’:

package com.softwarepassion.tutorial.camel;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;

/**
* A Camel Router
*/

public class MyRouteBuilder extends RouteBuilder {

public void configure() {

// TODO create Camel routes here. For example:-
//
// from("activemq:test.MyQueue").to("file://test");

}
}

with the following:

package com.softwarepassion.tutorial.camel;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;

/**
* A Camel Router
*/

public class MyRouteBuilder extends RouteBuilder {

public void configure() {
from("timer://tutorial?fixedRate=true&amp;delay=3000&amp;period=10000")   // 1
.setBody(constant("")).setHeader(Exchange.HTTP_METHOD, constant("POST"))                           // 2
.to("http://twitter.com").convertBodyTo(String.class).process(new Processor(){

public void process(Exchange exchange) throws Exception {
Object body = exchange.getIn().getBody();
System.out.println("BODY: " + body);
}

});
}

}

This route executes the timer every 10 seconds, fetches the ‘softwarepassion.com’ web page, convert the received message body to plain String and uses a Processor do display the content on the console.

I hope this will help someone when starting with ServiceMix ESB + Camel

You can download the full sources from the git repository at: https://github.com/softberries/esb_with_camel

2 responses on “Apache ServiceMix (Fuse ESB) with Camel

  1. Michael Kelly December 14, 2012 at 1:54 am

    Worth noting, this wasn’t working for me in the current version of ServiceMix until I renamed the SA zip file to jar

Leave a Reply