Welcome to EMC Consulting Blogs Sign in | Join | Help

Syam.Sathyan

Writing Camel Routes Using Spring DSL based XML (Apache-Camel Blog 3)

 

This is the third blog in the blog series Apache-Camel. In my last two blogs in this series I explained the Basic Introduction to Camel, Maven Project Setup for a sample camel spring dsl project etc. In this Blog I will try to describe the basic structure of a Spring DSL based Xml route which uses the following camel constructs and components.

  • Content Based Router- <choince>, <when> and <otherwise>
  • Transformation to data - applying simple transformations to data using <transform>
  • Dynamic expressions and contant values using <simple> and <constant> camel constructs.
  • Xpath expression for data / message parsing and searching.

Every camel route starts with a <route> tag. And the next most important thing inside a route is the <from> tag. The <from> tag defines the incoming or from end point of a route. There are cases where the <from> can be an auto start by camel's scheduler or in a web services world it can be a simple http servlet incoming request. Let's take a look at one of the simple routes that we wrote in the last blog (Apache Camel Blog2).

<route>

      <from uri="servlet:///hello"/>

            <choice>

                <when>

                    <header>name</header>

                     <transform>

                         <simple>Hello ${header.name} how are you?</simple>

                    </transform>

                </when>

                <otherwise>

                    <transform>

                        <constant>Add a name parameter to uri, eg ?name=foo</constant>

                    </transform>

                </otherwise>

            </choice>

        </route>

 

The ‘from' in this case is a servlet in coming request from an http client. The client should refer to this route by /hello .  Let's take a close look at the conditional filtering and transformations applied to the incoming request.

The Steps that follows after the route is invoked by an http request

  • 1. Any http request with /hello in the context path invokes and executes the route.
  • 2. The route sends the request to the <choice> construct.
  • 3. The when condition checks for any headers with name ‘name'.
  • 4. If the request contains such a header the <simple> expression is used to construct a new message with the value in the ‘name' header and the request body is transformed by the <transform> construct.
  • 5. If the request does not contain the ‘name' header then the request body is transformed to an error message with a <constant> construct.

The above example shows us the usage of choice, when, otherwise filtering mechanisms. Also it uses <transform> tags to achieve http request body transformation. There are no ‘to' tags in the above route. The route simply handles an incoming request takes decisions based on the filter transforms the body and sends it back, the sending back of information is pretty much behind the scenes and camel takes care of it at the end of the route tag and redirects the data back to the calling/invoking party.

Content Based Router <choice>          

As I mentioned in the first blog, camel heavily uses EIP (Enterprise Integration Patterns) and most of the camel constructs are based on one of the EIP's. Content based routing is a filtering pattern that can route an incoming message to the correct destination based on its contents. The figure below is taken from the camels Content Based Router page.

ContentBasedRouter

 

A Real Case Example (Quality of Service - QOS service switch using camel)
In a real case example the choice element can be accompanied by multiple <when> filters. The below example is a real case scenario where a different service has to be invoked based on the customers membership. And the requirement mandates the service consuming client to be immune to the underlying service url difference. Camel can easily handle that.

<choice>
     <when>
        <xpath>$membership = 'gold'</xpath>
             <to uri="http://mybogusservice.net/member/gold"/>
        </when>
        <when>
             <xpath>$membership = 'silver'</xpath>
             <to uri="http://mybogusservice.net/member/regular"/>
         </when>
         <otherwise>
             <to uri="http://mybogusservice.net/notsupported"/>
         </otherwise>
</choice>

In the above example the 'membership' variable value within a service request message is parsed using xpath expression to filter the gold and silver members and to serve them with dedicated service lines, other members are denied of the service with a 'not supported page'. In this route first time we are using a <to> tag for calling another http based web service from within a route. This example is classy in many ways - its simple, serves the purpose and easy to explain. Trust me, I have not tried this after all the simplification and trimming I did to this example for the sake of the reader. I removed a couple of headers and urls in it due to confidentiality and security concerns.  

Oops.. my job security concern!
And for sure I cannot jeopardize my job by showcasing this to my manager and educate him how easy this route was to implement compared to the actual time I took :), just kidding!.

In the next and the fourth blog in this series I will try to give more in-depth information about <simple> language expressions. For now this is good enough for one to start a basic camel maven project, setup a route config and write a simple message router!. Happy Routing.

Disclaimer

"The opinions expressed here are my personal opinions.  Content published here is not read or approved in advance by EMC and does not necessarily reflect the views or opinions of EMC."

Published 29 December 2011 14:06 by Syam.Sathyan

Comments

No Comments
Anonymous comments are disabled

About Syam.Sathyan

I am a simple, social, God fearing, tech savvy engineer who spends my free time investigating security loop holes in encryption algorithms, Optimizing android operating system, hacking linux kernel and writing 3D and 2D routines for my own mobile games. I worked for Quadrovision in Amsterdam, MMP in Bangalore, Panasonic in Malaysia etc. I am from Kerala (The most beautiful and yes the only Beef eating state in India).
Powered by Community Server (Personal Edition), by Telligent Systems