Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java tip: Write an SOA integration layer with Apache Camel

Web services integration with Spring and Apache Camel

  • Print
  • Feedback

In this Java tip, learn how to use Apache Camel to develop a quick-and-easy integration layer for a service-oriented architecture. Apache Camel lets you configure Camel integration components for web service endpoints; message transformation, routing, and aggregation; and exception handling. All with a minimum of Java coding.

Introduction

Integration is a top priority for enterprise projects that seek to connect multiple web service endpoints quickly, efficiently, and maintainably. From a development standpoint, integration can also be a serious challenge. One way to ease that challenge is by using an integration framework like Apache Camel.

Apache Camel's API and out-of-the-box components implement many common enterprise integration patterns (EIPs), making it relatively simple and easy to do integration tasks such as connecting web services, performing XSL transformations, logging audits, and more. In this Java tip we introduce a process for using Apache Camel as an integration solution. We start with a business integration problem involving multiple web services. We then map the problem, briefly discuss the components needed to resolve it, and implement a solution based on Apache Camel's routing engine.

An enterprise integration scenario

Say that we are given an airline portal that wants to develop a web service for its online reservation service. The service will aggregate fare quotes from multiple airline companies in order to provide portal visitors searching for flights with the best available fares. This scenario involves connecting multiple web services: The portal will invoke a ReservationService to collect a list of quotes based on the given search criteria. The portal ReservationService will then invoke the ReservationServices exposed by various airlines, consolidate the quotes, and return the results to the portal user. This is a typical integration scenario where a composite web service invokes multiple external web services and aggregates the responses.

Figure 1 shows a flow diagram of an SOA architecture for the portal reservation service and two airline reservation services. (Note that we are using a contract-first or Design by Contract for this demonstration.)

Figure 1. Flow diagram of an SOA architecture (click to enlarge)

Solving the problem with Java

Creating a custom solution for this business problem would mean writing a lot of Java code. We would need to map and transform the Request objects from their source formats to the target format; use Java threads to invoke both of the airline web services in parallel; set up event listeners to wait for both services to respond; aggregate the responses; and also manage supporting tasks like logging and exception handling.

Using Apache Camel as an integration framework shortcuts a lot of that programming. We can simply leverage Camel's API and components, which are designed to resolve most web services integration tasks with standard enterprise integration patterns (EIPs). Rather than Java code, we'll work with a streamlined domain-specific language (DSL). Apache Camel provides DSLs based on Java, Scala, Groovy, or XML. In this case, we'll use Camel's XML-based Spring DSL to specify the routing and configure our integration layer.

  • Print
  • Feedback

Resources
  • Download the complete source code for this article.
  • Read more articles in JavaWorld's Java tips series.
  • "Hello Camel: Automatic File Transfer" (Dustin Marx, JW Blogs, January 2013): Get another quick introduction to Apache Camel, this time using it to transfer files automatically from one specified directory to another.
  • "Open source Java projects: Vert.x" (Steven Haines, JavaWorld, July 2013): If you're challenged by enterprise integration you might want to check out Vert.x, an enterprise messaging and integration framework similar to Node.js, that runs on the JVM.
  • "Open Source Integration with Apache Camel and How Fuse IDE Can Help" (Jonathan Anstey, Dzone, May 2011): Try another hands-on implementation example, this time with graphical tooling from the Fuse IDE in the mix.
  • Learn more about Camel from the classic book by Claus Ibsen and Jonathan Anstey: Camel in Action (Manning Publications, December 2010 ).