First of all, you have to use the maven archetype servicemix-camel-service-unit
to create add a tutorial-camel-su
to your project. Check this page if you need help in running the archetype.
This archetype creates a service unit module with:
- the correct <dependency/> already in place
- a
camel-context.xml
file and an example of aRouteBuilder
class
Afterwards, add the new service units to the service assembly by adding the necessary <dependency/> element to the SA's pom.xml. If you want, you can also change the <name/> to get a cleaner build log.
Configuring camel-context.xml
Apache Camel supports configuration through XML files, but for this tutorial we are going to use the Java Domain Specific Language. Modify your camel-context.xml
file to point to the package containing the RouteBuilder
classes as in the example below
<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://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd"> <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring"> <package>org.apache.servicemix.tutorial</package> </camelContext> </beans>
Create your RouteBuilder
Now, create your own class to implement the Camel route and add this code to it:
package org.apache.servicemix.tutorial; import javax.xml.transform.dom.DOMSource; import org.apache.camel.builder.RouteBuilder; /** * Camel Router for the ServiceMix tutorial * * @version $Revision: 1.1 $ */ public class MyRouteBuilder extends RouteBuilder { public void configure() { from("jbi:service:urn:servicemix:tutorial:wiretap") .convertBodyTo(DOMSource.class) .to("jbi:endpoint:urn:servicemix:tutorial:jms:myQueue", "jbi:endpoint:urn:servicemix:tutorial:file:sender"); } }
Although the code pretty much speaks for itself, let us take a deeper look at it:
- the
from("jbi:service:urn:servicemix:tutorial:wiretap")
defines the starting point of our route and creates a matching JBI endpoint that we can use from our other SUs (in our case, thetutorial-file-su
refers to it) - by default, the normalized message sent by the
<file
:poller/>
will contain a StreamSource and for now we need to convert our message body to DOMSource ourselves to ensure re-readability by both target endpoint – this step will disappear with our next release - the
to("jbi:endpoint:urn:servicemix:tutorial:jms:myQueue", "jbi:endpoint:urn:servicemix:tutorial:file
:sender")
will send the message to both JBI endpoints mentioned
Build and deploy
If you use the instructions on this page to substitute for servicemix-eip
on 2.6. Beginner - Exercise, you should now be able to build and deploy your SA. The build output should look something like this:
[INFO] Scanning for projects... [INFO] Reactor build order: [INFO] Tutorial [INFO] Tutorial :: File SU [INFO] Tutorial :: JMS SU [INFO] Tutorial :: Camel SU [INFO] Tutorial :: SA ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ ...
Further reading
- Be sure to have a look at http://activemq.apache.org/camel for more information about Apache Camel
- The wire tap pattern is described on http://activemq.apache.org/camel/wire-tap.html and a full list of enterprise integrations patterns can be found at http://activemq.apache.org/camel/enterprise-integration-patterns.html.
- This page gives you more information about the
jbi:
URI we use to integrate between ServiceMix and Camel