Java Tip: Write an SOA integration layer with Apache Camel

Web services integration with Spring and Apache Camel

1 2 3 4 5 6 Page 6
Page 6 of 6

 Step 7: Create the Camel Context XML

In this step we create the camel-context XML. We add the processors, beans, routes, and endpoints in the camel-context.xml.

You might recall that we've used processors in our Camel routes to do various tasks. For instance, in Step 3 we used a Camel processor to add a header to the Camel Exchange, in order to conditionally execute our system flow and invoke a response from one or more desired airline web service(s). In this case the processor extracted the preference from the request XML and set the header, allowing us to configure this rule in a Camel route.

Additionally, in Step 4 we used a Camel processor to change the message into a format suitable for the XSLT component, and we used a Camel processor in the onException block in that route to extract the exception object and create a SOAP fault. In Step 6 we used a Camel processor to log the message into a data store. See the source code for a detailed listing of the processors.

Listing 6. camel-context.xml

<beans xmlns=""

    //Camel CXF endpoints used in Step 2
    <cxf:cxfEndpoint ...

    //Java beans and Camel Processors used in Steps 3,4 and 6
    <bean id="requestValidator" class="com.sample.processor.RequestValidator" />

    //the camel-context that encloses the Camel routes defined above
    <camel:camelContext id="camelContext"
    //the routes defined above

In Listing 7 we create the web.xml to load camel-context and deploy the application as a WAR file.

Listing 7. web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" xmlns=""

    <display-name>Camel Routes</display-name>



    //camel context xml

    //the listener that kickstarts Spring 

 Step 8 : Build and deploy the WAR file

We are now ready to deploy our Camel SOA integration layer. First we'll add the necessary dependencies to our project's pom.xml (see the article source code), then we'll use our Maven POM script to build the application.

Executing the Maven POM script with cmd : mvn install generates a WAR file. Repeat this for the three projects: ReservationService, ReservationServiceAirlineA, ReservationServiceAirlineB. The script compiles the Java code and copies the camel-context.xml from Listing 6, the XSLT sheets, and the compiled Java classes into the WEB-INF/classes folder in the WAR file. We can then deploy and run the integration layer in a web server like Tomcat.

To deploy the sample app on a Tomcat server, simply copy the WAR files generated to the webapps folder of your server and start it. Next, access the WSDL of the deployed service at the following URL:


Use the WAR files to deploy the mock web services for Airline A and Airline B. The WSDL URLs will be:




Use the request XML of the reservation service in the source code to check out these web services.


In this Java tip we've shown you briefly how to use Apache Camel to build an integration layer in an SOA web services implementation. We used the Camel CXF PAYLOAD data format for direct access to the raw XML passed between web services, and XSLT sheets to do the request-and-response transformation. This approach removed the need for XML-to-Java binding, with XML validation happening at the CXF endpoint or in the XSLT as needed.

We also demonstrated Camel's implementation of the Multicast and Aggregator integration patterns, which we used to route a single web service request to multiple endpoints in parallel and aggregate the responses. We used the Camel wireTap component to log messages at points of interest in our system. We also used the onException block in our Camel route to handle exceptions and throw SOAP faults, as necessary. Using Camel's Spring DSL, we were able to configure these components using XML, with very little Java code. See the Resources section to learn more about Apache Camel and download the source code for the demo.

Anirban Konar has more than 17 years of experience in IT and has designed and developed Java-based solutions since 2001. A Sun Certified Java Programmer and Enterprise Architect, Anirban currently works as a senior architect for Cognizant Technology Solutions, India.

Devaradjan Venkatesan has over eight years of experience in IT and currently works as a technology specialist for Cognizant Technology Solutions, India.

Learn more about this topic

This story, "Java Tip: Write an SOA integration layer with Apache Camel " was originally published by JavaWorld.

Copyright © 2013 IDG Communications, Inc.

1 2 3 4 5 6 Page 6
Page 6 of 6