Simple Camel DSL OSGi Bundle example

Here’s a simple example how you can create a Camel Route using it’s DSL, and deploy it as an OSGi bundle in the ServiceMix Kernel.

I’ll start with the java class that builds the camel routes. The following class, nl.iteye.camelbundle1.FileRouteBuilder configures a route that will copy any file from /tmp/src to /tmp/dest.

package nl.iteye.camelbundle1;

import org.apache.camel.builder.RouteBuilder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FileRouteBuilder extends RouteBuilder {
private static final transient Log LOG = LogFactory.getLog(FileRouteBuilder.class);

public void configure() {
LOG.info("configuring routes");
from("file:///tmp/src").to("file:///tmp/dest");
}
}

I’m going to create an OSGi bundle which can be deployed in the Servicemix kernel. An OSGi bundle is basically a jar file with a special manifest file. The following code shows the META-INF/manifest.mf file.

Manifest-Version: 1.0
Bundle-Name: camel-bundle-1
Bundle-SymbolicName: camel-bundle-1
Bundle-Version: 1.0.0
Bundle-Description: Camel Bundle 1
Bundle-Vendor: IT-eye
Bundle-Category: example
Import-Package: org.osgi.framework, org.apache.commons.logging, org.apache.camel.builder, org.apache.camel.model

When you deploy a bundle in Servicemix, Spring looks for META-INF/spring/*.xml files which can contain camel routes. In the following file i’m telling spring to look in the package nl.iteye.camelbundle1 for classes. This way my FileRouteBuilder class will automatically be run when the bundle is started.

<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.5.xsd
http://activemq.apache.org/camel/schema/spring
http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">
    <camelContext xmlns="http://activemq.apache.org/camel/schema/spring" >
        <package>nl.iteye.camelbundle1</package>
    </camelContext>
</beans>

Next I’m using a simple ant script to compile and jar the above files. The resulting jar can be installed in ServiceMix.

Before you can install the bundle, you need to install some required bundles, as is described here: Apache ServiceMix Kernel and Camel. You can verify the bundles installed correctly using osgi list command in ServiceMix kernel. The result should show the following modules:

[  29] [Active     ] [   50] camel-core (1.4.0)
[  30] [Active     ] [   50] camel-spring (1.4.0)
[  31] [Active     ] [   50] spring-tx (2.5.5)

Now i can install the bundle i’ve created as follows:

servicemix> osgi install file:///tmp/osgi/camel-bundle-1.jar
Bundle ID: 68
servicemix> osgi start 68

Test it by creating a file in /tmp/src.

blog comments powered by Disqus