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.
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.
Create a new Mavenized Camel routing project from the
org.apache.camel.archetypes:camel-archetype-spring
archetype. Name itcamel-rou
te
.Change your working directory to the new project (
cd camel-route
).Build and install the project by executing the
mvn install
Maven command.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 theSERVICEMIX_HOME/deploy
directoryThe 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
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&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
)
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.
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).
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.