Andrej Koelewijn

  • Home
  • About
  • Publications
  • Departments
    • agile
    • architecture
    • cloud
    • java
    • mobile
    • open standards
    • oracle
    • oss
    • other
    • soa
    • software development
    • tablet
    • Uncategorized
    • web
  • Subscribe via RSS

A composite Rest service using Apache Camel

June 13th, 2010  |  Published in java, oss, soa, web  |  5 Comments

The following example illustrates how you can implement a composite service using Apache Camel. The service is exposed as a REST resource, and it uses two other resources to collect the data required.

The composite service will enable a client to get info on a customer and it’s orders. To get this data the composite service first needs to query a customer service. Using a relation id retrieved from the customer service, the composite then needs to query an orders services. The steps are as follows:

  • Client calls the composite, eg., GET http://localhost/customerOrders?custNo=123
  • The composite gets the customer resource, but this resource expects the customer id to be named differently: GET http://localhost/customer?customer=123. Here’s an xml representation of the resource returned:
    <customer
      custNo='123'
      relationId='443'
      firstName='Peter'
      lastName='Semper'
    />
    
  • Next the composite needs to use the relationId in the customer resource to get it’s orders, e.g., GET http://localhost/orders?relId=443. This will result in the following xml document:
    <orders relationId='443'>
      <order id='1' product='Car'/>
      <order id='2' product='Bicycle'/>
    </orders>
    
  • The composite service will use the data returned by the customer resource and the order resource to create a new xml document containing both the customer info and the order info:
    <customer>
      <id>123</id>
      <orders>
        <order><id>1</id><product name="Car"/></order>
        <order><id>2</id><product name="Bicycle"/></order>
      </orders>
    </customer>
    

Here’s the sequence diagram illustrating the steps outlined above.

You can achieve this with the following route:

        from("jetty:http://localhost:9012/customerOrders")
                .removeHeaders("CamelHttp*")
                .setHeader("customer",header("custNo"))
                .setHeader(Exchange.HTTP_METHOD,constant("GET"))
                .to("http://localhost:9012/customer")
                .removeHeaders("CamelHttp*")
                .setHeader("relId", xpath("/customer/@relationId",String.class))
                .setHeader(Exchange.HTTP_METHOD,constant("GET"))
                .enrich("http://localhost:9012/orders",aggregate)
                .to("xquery:merge-customer-orders.xquery")
                .setHeader(Exchange.CONTENT_TYPE,constant("text/xml"));

Line by line explanation:

  1. Define an endpoint where the composite service is reachable. I’m using a jetty component to listen for requests.
  2. Remove the http headers from the message received, so they won’t be used for calls from the composite to the other services.
  3. Set the customer parameter for the http call to /customer.
  4. Specify that we’re GETting the customer resource.
  5. Get the customer resource.
  6. Again, remove any http headers, so we’re not messing up the next http call.
  7. Use an xpath expression to determine the relation id of the customer, set the query parameter used to GET the orders for this relation.
  8. Specify that we’re GETting the orders resource.
  9. GET the orders resource, and then use the aggregate object to combine customer and orders. In this example the aggregate object just concatenates both xml documents, so we can easily use it in the next step.
  10. Use an xquery to create a new xml document from the combined customer and orders document.
  11. Set the Http header for Content-type to text/xml, so the client knows it’s receiving an xml representation of the resource.

The aggregate object takes the message (exchange) received from the customer resource and the message received from the orders resource and concatenes them into one xml document:

AggregationStrategy aggregate = new AggregationStrategy(){
  @Override
  public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    newExchange.getOut().setBody("<all>"
      + oldExchange.getIn().getBody(String.class)
      + newExchange.getIn().getBody(String.class)
      + "</all>");
    return newExchange;
  }
};

The combined xml document can easily be used in an xquery to build a new xml document:

<customer>
  <id>{data(/all/customer/@custNo)}</id>
  <orders>
    { for $order
      in  /all/orders/order
      return <order><id>{data($order/@id)}</id><product name='{data($order/@product)}'/></order>
    }
  </orders>
</customer>

That’s most of the code you need. You’ll have to put the route configuration into a route builder and start a camel context to run the route:

    public static void main(String args[]) throws Exception {
        CamelContext ctx = new DefaultCamelContext();
        ctx.addRoutes(new CustomerOrdersResourceRouteBuilder());
        ctx.start();
    }

The nice thing about camel is that’s easy to test and debug. You can simply start the main class in your IDE and give the route a try. You can use the same approach to unit test the route. No need to do a complete deployment to some ESB server.

Another advantage of using Camel is that all your endpoints are normal strings in some java code that can easily be made configurable, for example using a property file

  • tarjeihuse

    This is a very cool example, one thing I get when trying to use Camel groovy in a similar way is that camel just stops after camelContext.start() returns. Any idea why?

  • Dennis

    Nice article but would be even better if the sample code was downloadable

  • http://www.andrejkoelewijn.com/ andrej

    It's on github: http://github.com/ako/ako-soabp/tree/master/Cus…

  • Anonymous

    Nice blog post. Your service has many similarities to a composite REST service I’ve implemented using Apache Camel. Difference with your service is that I’m using the camel-servlet component to offer a REST interface (since it’s running inside a servlet container).

    My service joins together data from four different systems. It extracts data from a SOAP-based webservice (of one application) and merges it with similar data from three different databases (no other interface available for these systems). The database records are mapped to Java objects and transformed to XML by FreeMarker. It works out quite nice, a complete service
    description it just a few lines of code. The whole service is tested through a unit/integration test build using standard JUnit/Spring.

  • Josebetomex

    You save my day

    I was finding how to change mime-type in restlet

    exchange.getOut().setHeader(Exchange.CONTENT_TYPE, “text/xml”);

    Work for me

    Tranks

    Alberto Garcia

Tags

activemq agile bi camel css esb google governance grails groovy gtalk html5 internet ipad ivy J2EE java javascript jaxrs jmx jquery lean linkeddata linux maven mule noiv openoffice opensource Open Source oracle osgi oss rdbms rest scrum servicemix soa sql svg tablet web 2.0 XML xmpp yql

Archives

  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • June 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Recent Posts

  • Self-service architecture to avoid water-scrum-fall
  • Updating a vagrant box
  • Using littleproxy in Mule unit tests
  • Useful site to determine what html5, css3 & svg you can use
  • A Product Owner is a Project Manager

Categories

  • agile
  • architecture
  • cloud
  • java
  • mobile
  • open standards
  • oracle
  • oss
  • other
  • soa
  • software development
  • tablet
  • Uncategorized
  • web

Recent Comments

  • From DevOps to NoOps to NoCollabs :: Andrej Koelewijn on LAC2011 presentation: Scrum under Architecture
  • andrej on Software architecture, PHP and Javascript
  • Pcdiggs on Software architecture, PHP and Javascript
  • Een Scrum Product Owner is een Project Leider on A Product Owner is a Project Manager
  • Using css webfonts in inkscape :: Andrej Koelewijn on Create presentations using inkscape
Buzz
andrkoel: Utrecht hele dag mist, scheveningen zomerse dag... http://t.co/cDRCJHr9
4:35 PM Nov 10, 2011, comment
andrkoel: RT @stefanvdkamp: Beter filmpje van Garret McNamara in de 30 meter hoge golf. http://t.co/9abiWkYX
9:20 AM Nov 10, 2011, comment
andrkoel: Mmm, een uur voor den haag - leiden lijkt te weinig, ga mijn afspraak niet redden...
7:34 AM Nov 10, 2011, comment
andrkoel: Big Data is Useless without Science http://t.co/2I8EiLsH
6:18 AM Nov 10, 2011, comment
andrkoel: Just tried #vagrant to quickly setup virtualbox development environment. Looks good, although provisioning apache through puppet failed...
10:47 PM Nov 09, 2011, comment
 


©2012 Andrej Koelewijn
Powered by WordPress using the Gridline Lite theme by Graph Paper Press.