Book Image

Instant Apache ServiceMix How-to

By : Henryk Konsek
Book Image

Instant Apache ServiceMix How-to

By: Henryk Konsek

Overview of this book

<p>Creating reliable integration solution can be easy if you choose the right tools for the job. Apache Camel and ServiceMix are the leading integration technologies dedicated to dealing with the complexity of the messaging solutions. Learn how to efficiently integrate multiple systems with bleeding edge open source Apache software.</p> <p>"Instant Apache ServiceMix How-to" is a practical, hands-on guide that provides you with a number of clear, step-by-step exercises that will help you take advantage of the real power that is behind the leading Apache integration stack.</p> <p>This book guides the reader in how to install ServiceMix and how to get it up and running. It will take you through a number of practical recipes – starting with the basic commands of Apache Karaf container and ending with the deployment of JMS and web service solutions.</p>
Table of Contents (7 chapters)

Creating and deploying a new Camel route (Must know)


The most common type of module deployed to ServiceMix is probably the Camel-based routing bundle. This recipe will guide you through the process of creating and deploying a new Camel route to the ServiceMix container.

Getting ready

To create a new Camel routing project, we will use the Maven Archetype mechanism (http://maven.apache.org/archetype/maven-archetype-plugin), so make sure that you have Maven properly installed and available in your command-line path.

We will also assume that your ServiceMix instance is already running.

How to do it...

  1. Create a new Mavenized Camel routing project from the org.apache.camel.archetypes:camel-archetype-spring archetype. Name it camel-rou te.

  2. Change your working directory to the new project (cd camel-route).

  3. Build and install the project by executing the mvn install Maven command.

  4. Deploy the project to ServiceMix by copying the JAR from the camel-route/target (camel-route/target/camel-route-1.0-SNAPSHOT.jar) directory to the SERVICEMIX_HOME/deploy directory

  5. The command needed to create a new project from the the Camel Maven archetype (step one) is presented as follows:

    mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-spring -DgroupId=com.packtpub.servicemixhowto -DartifactId=camel-route -Dversion=1.0-SNAPSHOT
    

How it works...

We used the Maven Archetype mechanism to create a new Camel routing project. Archetypes are basically the templates of the projects that can be used to generate the stub of new Maven modules. In this particular case, we create the stub of the Camel routing module.

The Camel module we have created contains the Spring configuration file used to bootstrap it by ServiceMix. The mentioned Spring XML wiring file is located at camel-route/src/main/resources/META-INF/spring/camel-context.xml. By default, this file contains an initial Camel Context configuration and some sample routes that can be used as a base for your custom routing rules. Feel free to experiment a little bit and change the route to make it suitable for your needs. For example, in order to create the Camel route that sends a message to ServiceMix log every 5 seconds, you could replace the Camel Context definition in the camel-context.xml file with the following route:

<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
  <camel:route>
     <camel:from uri="timer://myTimer?fixedRate=true&amp;period=5000"/>
     <camel:to uri="log:EXAMPLE-ROUTE"/>
   </camel:route>
</camel:camelContext>

In the preceding example, we used the Timer (http://camel.apache.org/timer.html) and Log (http://camel.apache.org/log.html) components of Apache Camel. To redeploy the route, just build it again with Maven (mvn install) and replace the file in SERVICEMIX_HOME/deploy with the new version of the generated artifact (camel-route/target/camel-route-1.0-SNAPSHOT.jar)

There's more...

Apache Camel is a Swiss army knife for message routing and system integration. Since this book is focused only on ServiceMix, we will suggest you find more information about Camel itself. We will also write some additional information about how Camel cooperates with Karaf.

Camel references

If you are not sure how to create proper routes in Camel, don't hesitate to learn more about it. Keep in mind that you cannot effectively use ServiceMix without having some solid Camel knowledge. There are two main sources of information about Apache Camel: The project documentation site (http://camel.apache.org/documentation.html) and the excellent Camel In Action book from Manning Publishing (http://www.manning.com/ibsen).

Camel Context per module

When you deploy the Camel routing module to the ServiceMix instance, Karaf will create a new Camel context for you. It is important to remember that ServiceMix doesn't operate on the single Camel Context shared by multiple routes, but rather on a set of small context instances. This is a preferable way of dealing with Camel routing in ServiceMix, since it leverages the modularity provided by the OSGI technology.