ServiceMix (FuseESB) - Camel OSGI Example - Part 4

ServiceMix (FuseESB) – Camel OSGI Example – Part 4

As I have encountered problems developing further my ServiceMix ESB application (you can read about it here) I was forced to switch from the JBI style to pure OSGI (at this point I have to say that the previous parts were mainly based on the information from ServiceMix website which I have to say is a ‘bit’ outdated).
Osgi? Well, even better :).

Lets start off with the simple OSGI example, you would have to follow at least Part 1 of this series to create basic environment for sending messages to JMS queue on Apache Active MQ.

Our application will pick up the messages from one queue, split them using Splitter EIP into separate messages, set header attribute on each of them and then, process them separately and aggregate them (Aggregator EIP) into a single block of text and print it on the log console.

This time we are not going to create service assembly or any service unit project, we are going to create simple maven artefact and modify its pom.xml file to include necessary dependencies, plugin definitions and packaging type, thats it!

So, first of all start up your eclipse or any other editor (you can do this from the command line too) and create basic maven project:

For eclipse it would be: ‘File’ -> ‘Other’ -> ‘Project’ then select ‘Maven Project’

click ‘Next’, select checkbox with ‘create a simple project’

click ‘Next’, fill up the fields for your newly created artifact and finish

Replace automatically created pom.xml file with the following:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.apache.servicemix.features</groupId>
        <artifactId>features</artifactId>
        <version>4.4.1-fuse-01-11</version>
    </parent>

    <groupId>com.softwarepassion.camelosgitut</groupId>
    <artifactId>camelosgitut</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>CamelOsgiTut</name>
    <description>Camel OSGI Tutorial</description>
    <packaging>bundle</packaging>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                        <Bundle-Description>${project.description}</Bundle-Description>
                        <Import-Package>*</Import-Package>
                        <Private-Package>org.apache.servicemix.examples.camel
                        </Private-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <!-- FuseSource maven repositories -->
        <repository>
            <id>fusesource.releases</id>
            <name>FuseSoure releases repository</name>
            <url>http://repo.fusesource.com/maven2/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>fusesource.snapshots</id>
            <name>FuseSource Snapshot Repository</name>
            <url>http://repo.fusesource.com/maven2-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <!-- FuseSource maven repositories -->
        <pluginRepository>
            <id>fusesource.releases</id>
            <name>FuseSoure releases repository</name>
            <url>http://repo.fusesource.com/maven2/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>fusesource.snapshots</id>
            <name>FuseSource Snapshot Repository</name>
            <url>http://repo.fusesource.com/maven2-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>
</project>

Now you are ready to build our project for the first time. Execute ‘mvn install’ inside the project directory or from eclipse ‘Run As..’ -> ‘Maven Install’

Bravo! you have created osgi bundle ready to deploy into karaf. Let’s do some coding for our camel logic.
Create two classes inside src/main/java folder, one for Camel Routes definitions using Java DSL and one for managing our aggregation strategy (when we will aggregate our messages into one after the split)

MyRouteBuilder.java

package com.softwarepassion.camelosgitut;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.Main;

/**
 * A simple example router to show how to define the route with Java DSL
 *
 * @version $Revision$
 */

public class MyRouteBuilder extends RouteBuilder {
    /**
     * Allow this route to be run as an application
     *
     * @param args
     */

    public static void main(String[] args) throws Exception{
        new Main().run(args);
    }

    public void configure() {
        from("activemq:test2").split(xpath("/notes/note")).parallelProcessing().setHeader("id", simple("123")).to("activemq:test3");
        from("activemq:test3").aggregate(header("id"), new MyAggregationStrategy()).completionTimeout(3000).to("log:ExampleRouter");
    }

   
}

MyAggregationStrategy.java

package com.softwarepassion.camelosgitut;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.processor.aggregate.AggregationStrategy;

public class MyAggregationStrategy implements AggregationStrategy {

    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        if (oldExchange == null) {
            return newExchange;
        }
        Message newIn = newExchange.getIn();
        String oldBody = oldExchange.getIn().getBody(String.class);
        String newBody = newIn.getBody(String.class);
        newIn.setBody(oldBody + newBody);
       
        return newExchange;
    }

}

The last, very important thing we have to do is to install Java DSL route builder. Add beans.xml file into META-INF directory of the resources folder, the full path where the beans.xml file should be located is: ‘/camelosgitut/src/main/resources/META-INF/spring’.

beans.xml

<?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:ctx="http://www.springframework.org/schema/context"
      xmlns:camel="http://camel.apache.org/schema/spring"
      xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
      http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
      http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd">


  <camel:camelContext xmlns="http://camel.apache.org/schema/spring">
    <!-- install the Java DSL route builder -->
    <package>com.softwarepassion.camelosgitut</package>
  </camel:camelContext>
</beans>

Build the application one more time and deploy the .jar file from the target directory into ‘deploy’ directory of the servicemix runtime:

cp target/camelosgitut-0.0.1-SNAPSHOT.jar /home/kris/apache-servicemix-4.4.1-fuse-01-11/deploy/

Now you are ready to send our message to the queue named ‘test2′. Create a simple xml message which would be splitted into smaller parts:

<?xml version="1.0" encoding="UTF-8"?><notes><note><to>Anyone</to><from>Kris1</from><heading>Simple Message</heading><body>Check out my Twitter account @grajo</body></note><note><to>Anyone</to><from>Kris2</from><heading>Simple Message</heading><body>Check out my Twitter account @grajo</body></note><note><to>Anyone</to><from>Kris3</from><heading>Simple Message</heading><body>Check out my Twitter account @grajo</body></note></notes>

This message should be split into 3 smaller messages, sent in parallel to the separate queue and there aggregated into single block of text.

One response on “ServiceMix (FuseESB) – Camel OSGI Example – Part 4

Leave a Reply

Related Posts

    No Releated Posts

One response on “ServiceMix (FuseESB) – Camel OSGI Example – Part 4

Leave a Reply