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="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:cxf="http://camel.apache.org/schema/cxf" 
    xmlns:camel="http://camel.apache.org/schema/spring"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://camel.apache.org/schema/spring 
       http://camel.apache.org/schema/spring/camel-spring.xsd
       http://camel.apache.org/schema/cxf 
       http://camel.apache.org/schema/cxf/camel-cxf.xsd>

    //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"
        xmlns="http://camel.apache.org/schema/spring">
    //the routes defined above
    <route>
    ...
    </route>
    <route>
    ...
    </route>
    </camelContext>
</beans>

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="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

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

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>


    //camel context xml
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/camel-context.xml</param-value>
    </context-param>

    //the listener that kickstarts Spring 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>


 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:

http://localhost:8080/ReservationService-1.0-SNAPSHOT/QuoteService?wsdl

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

http://localhost:8080/cxf-sample-airlineA-1.0/services/AirLineAQuote?wsdl

and

http://localhost:8080/cxf-sample-airlineB-1.0/services/AirLineBQuote?wsdl

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

Conclusion

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

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