DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Spring Boot Microservices + Apache Camel: A Hello World Example
  • Introducing Stalactite ORM
  • Aggregating REST APIs Calls Using Apache Camel
  • REST Services With Apache Camel

Trending

  • Java’s Next Act: Native Speed for a Cloud-Native World
  • A Guide to Container Runtimes
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Spring Integration and Apache Camel

Spring Integration and Apache Camel

By 
Biju Kunjummen user avatar
Biju Kunjummen
·
Dec. 31, 09 · Interview
Likes (3)
Comment
Save
Tweet
Share
100.9K Views

Join the DZone community and get the full member experience.

Join For Free

Spring Integration and Apache Camel are open source frameworks providing a simpler solution for the Integration problems in the enterprise, to quote from their respective websites:


Apache Camel -

Apache Camel is a powerful open source integration framework based on known Enterprise Integration Patterns with powerful Bean Integration.

Spring Integration -
It provides an extension of the Spring programming model to support the well-known Enterprise Integration Patterns while building on the Spring Framework's existing support for enterprise integration.

Essentially Spring Integration and Apache Camel enable applications to integrate with other systems.

This article seeks to provide an implementation for an integration problem using both Spring Integration and Apache Camel. The objective is to show how easy it is to use these frameworks for a fairly complicated integration problem and to recommend either of these great products for your next Integration challenge.

Problem:

To illustrate the use of these frameworks consider a simple integration scenario, described using EIP terminology:

 

The application needs to get a "Report" by aggregating "Sections" from a Section XML over http service. Each request for Report consists of a set of request for sections – in this specific example there are requests for three sections, the header, body and footer. The XML over http service returns a Section for the Section Request. The responses need to be aggregated into a single report. A sample test for this scenario is of the following type:

        ReportGenerator reportGenerator = reportGeneratorFactory.createReportGenerator();
List<SectionRequest> sectionRequests = new ArrayList<SectionRequest>();

String entityId="A Company";

sectionRequests.add(new SectionRequest(entityId,"header"));
sectionRequests.add(new SectionRequest(entityId,"body"));
sectionRequests.add(new SectionRequest(entityId,"footer"));

ReportRequest reportRequest = new ReportRequest(sectionRequests);

Report report = reportGenerator.generateReport(reportRequest);
List<Section> sectionOfReport = report.getSections();
System.out.println(report);
assertEquals(3, sectionOfReport.size());

The “ReportGenerator” is the messaging gateway, hiding the details of the underlying messaging infrastructure and in this specific case also the integration API – Apache Camel or Spring Integration. To start with, let us implement a solution to this integration problem using Spring Integration as the Framework, followed by Apache Camel. The complete working code using Spring Integration and Apache Camel is also available with the article.

 

Solution Using Spring Integration:

The Gateway component is easily configured using the following entry in the Spring Configuration. Internally Spring Integration uses AOP to hook up a component which routes the requests from an internal input channel and waits for the response in the response channel.

<si:gateway default-reply-channel="exit" default-request-channel="enter" id="reportGenerator" service-interface="org.bk.report.ReportGenerator">
</si:gateway>

The component to Split the Input Report Request to Section Request is fairly straightforward:
public class SectionRequestSplitter {    
public List split(ReportRequest reportRequest){
return reportRequest.getSectionRequests();
}

}
and to hook this splitter with Spring Integration:
<bean class="org.bk.report.common.SectionRequestSplitter" id="sectionRequestSplitterBean">

<si:splitter id="sectionRequestSplitter" input-channel="enter" method="split" output-channel="sectionRequestToXMLChannel" ref="sectionRequestSplitterBean">

</si:splitter></bean>
Next, to transform the Section Request to an XML format - The component is the following:
public class SectionRequestToXMLTransformer {
public String transform(SectionRequest sectionRequest){
//this needs to be optimized...purely for demonstration of the concept
String sectionRequestAsString = "<section><meta><entityId>" + sectionRequest.getEntityId()
+ "</entityId><sectionName>" + sectionRequest.getSectionId()
+ "</sectionName></meta></section>";

return sectionRequestAsString;

}
}
and is hooked up in the Spring Integration configuration file in the following way:
<bean id="sectionRequestToXMLBean" class="org.bk.report.common.SectionRequestToXMLTransformer"/>
<si:transformer input-channel="sectionRequestToXMLChannel" ref="sectionRequestToXMLBean" method="transform" output-channel="sectionRequestChannel"/>
To send an XML over http request using the Section Request XML to a section Service:
<http:outbound-gateway id="httpHeaderGateway"
request-channel="sectionRequestChannel" reply-channel="sectionResponseChannel"
default-url="${sectionBaseURL}/section" extract-request-payload="true"
charset="UTF-8" request-timeout="1200" />

To transform the Section Response XML to a Section Object - The component is the following:
public class SectionResponseXMLToSectionTransformer {
public Section transform(String sectionXML) {
SAXReader saxReader = new SAXReader();
Document document;
String sectionName = "";
String entityId = "";
try {
document = saxReader.read(new StringReader(sectionXML));

sectionName = document
.selectSingleNode("/section/meta/sectionName").getText();
entityId = document.selectSingleNode("/section/meta/entityId")
.getText();
} catch (DocumentException e) {
e.printStackTrace();
}
return new Section(entityId, sectionName, sectionXML);
}
}
and is hooked up in the Spring Integration configuration file in the following way:
<bean id="sectionResponseXMLToSectionBean" class="org.bk.report.common.SectionResponseXMLToSectionTransformer" />
<si:transformer input-channel="sectionXMLResponseChannel"
ref="sectionResponseXMLToSectionBean" method="transform" output-channel="sectionResponseChannel" />


To aggregate the Sections together into a report, the component is the following::
public class SectionResponseAggregator {

public Report aggregate(List<Section> sections) {
return new Report(sections);
}

}

and is hooked up in the Spring Integration configuration file in the following way:
<bean id="sectionResponseAggregator" class="org.bk.report.common.SectionResponseAggregator"/>
<si:aggregator input-channel="sectionResponseChannel" output-channel="exit" ref="sectionResponseAggregator" method="aggregate"/>

This completes the Spring Integration implementation for this Integration Problem. The following is the complete Spring Integration configuration file:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:si="http://www.springframework.org/schema/integration"
xmlns:http="http://www.springframework.org/schema/integration/http"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-1.0.xsd
">

<context:property-placeholder />
<si:channel id="enter" />

<si:channel id="exit" />

<si:gateway id="reportGenerator" default-request-channel="enter"
default-reply-channel="exit" service-interface="org.bk.report.ReportGenerator" />

<si:channel id="sectionRequestToXMLChannel" />
<si:splitter id="sectionRequestSplitter" input-channel="enter"
ref="sectionRequestSplitterBean" method="split" output-channel="sectionRequestToXMLChannel" />

<si:channel id="sectionRequestChannel" />
<si:transformer input-channel="sectionRequestToXMLChannel"
ref="sectionRequestToXMLBean" method="transform" output-channel="sectionRequestChannel" />

<si:channel id="sectionXMLResponseChannel" />
<http:outbound-gateway id="httpHeaderGateway"
request-channel="sectionRequestChannel" reply-channel="sectionXMLResponseChannel"
default-url="${sectionBaseURL}/section" extract-request-payload="true"
charset="UTF-8" request-timeout="1200" />

<si:channel id="sectionResponseChannel" />
<si:transformer input-channel="sectionXMLResponseChannel"
ref="sectionResponseXMLToSectionBean" method="transform" output-channel="sectionResponseChannel" />

<si:aggregator input-channel="sectionResponseChannel"
output-channel="exit" ref="sectionResponseAggregator" method="aggregate" />

<bean id="sectionRequestSplitterBean" class="org.bk.report.common.SectionRequestSplitter" />
<bean id="sectionRequestToXMLBean" class="org.bk.report.common.SectionRequestToXMLTransformer" />
<bean id="sectionResponseXMLToSectionBean" class="org.bk.report.common.SectionResponseXMLToSectionTransformer" />
<bean id="sectionResponseAggregator" class="org.bk.report.common.SectionResponseAggregator" />

</beans>

 

A working sample is provided with the article(Download, extract and run "mvn test")

 

Solution using Apache Camel:

Apache Camel allows the route to be defined using multiple DSL implementations – Java DSL, Scala DSL and an XML based DSL. The recommended approach is to use Spring CamelContext as a runtime and the Java DSL for route development. The following is to build the Spring Camel Context:

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<template id="camelTemplate" />
<routeBuilder ref="routeBuilder"/>
</camelContext>


The route is configured by the Java based DSL:
public class CamelRouteBuilder extends RouteBuilder {
private String serviceURL;

@Override
public void configure() throws Exception {

from("direct:start")
.split().method("sectionRequestSplitterBean", "split")
.aggregationStrategy(new ReportAggregationStrategy())
.transform().method("sectionRequestToXMLBean", "transform")
.to(serviceURL)
.transform().method("sectionResponseXMLToSectionBean", "transform");
}

public void setServiceURL(String serviceURL) {
this.serviceURL = serviceURL;
}
}

Apache Camel does not provide an out of the box Message Gateway feature, however it is fairly easy to create a wrapper component that can hide the underlying details in the following way:

Reader davsclaus has provided references to two mechanisms with Apache Camel to provide an out of the box Messaging Gateway - Messaging Gateway EIP and Camel Proxy which allows a POJO to be used as a Mesaging Gateway. 

Camel Proxy will be used with the article, and can be configured in the Camel Configuration files in the following way:

	<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

		<proxy id="consumerMessageGateway" serviceInterface="org.bk.report.ReportGenerator"
			serviceUrl="direct:start" />

		<template id="camelTemplate" />
		<routeBuilder ref="routeBuilder" />
	</camelContext>

Per davsclaus, there is a bug in Apache Camel(2.1 or older) when invoking a bean later in the route(the splitter bean), which is to be fixed in Apache Camel 2.2. To work around this bug, a convertBody step will be introduced in the route:

        from("direct:start")
.convertBodyTo(ReportRequest.class)
.split(bean("sectionRequestSplitterBean", "split"), new ReportAggregationStrategy())
.transform().method("sectionRequestToXMLBean", "transform")
.to(serviceURL)
.transform().method("sectionResponseXMLToSectionBean", "transform");

 

The component to Split the Input Report Request to Section Request is exactly same as Spring Integration component:

public class SectionRequestSplitter {

public List<SectionRequest> split(ReportRequest reportRequest){
return reportRequest.getSectionRequests();
}

}
To hook the component with Apache Camel:
<bean id="sectionRequestSplitterBean" class="org.bk.report.si.SectionRequestSplitter" />

from("direct:start")
.split().method("sectionRequestSplitterBean", "split")
....
Next to transform the Section Request to an XML format, again this is exactly same as the implementation for Spring Integration, with hook being provided in the following manner:
......
.transform().method("sectionRequestToXMLBean", "transform")
......
To send an XML over http request using the Section Request XML to a section Service:
......
.transform().method("sectionRequestToXMLBean", "transform")
.to(serviceURL)
.........
To transform the Section Response XML to a Section object, the component is exactly same as the one used with Spring Integration, with the following highlighted hook in the Camel route:
......
.transform().method("sectionResponseXMLToSectionBean", "transform");

To aggregate the Section responses together into a report, the component is a bit more complicated than Spring Integration. Apache Camel supports a Scatter/Gather pattern using a route of the following type:
......
.split().method("sectionRequestSplitterBean", "split")
.aggregationStrategy(new ReportAggregationStrategy())
with an aggregation strategy being passed on to the Splitter, the aggregation strategy implementation is the following:
public class ReportAggregationStrategy implements AggregationStrategy {
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (oldExchange == null) {
Section section = newExchange.getIn().getBody(Section.class);
Report report = new Report();
report.addSection(section);
newExchange.getIn().setBody(report);
return newExchange;
}

Report report = oldExchange.getIn().getBody(Report.class);
Section section = newExchange.getIn().getBody(Section.class);
report.addSection(section);
oldExchange.getIn().setBody(report);
return oldExchange;

}
}
This completes the Apache Camel based implementation. A working sample for Camel is provided with the article - just download, extract and run "mvn test".


Conclusion:

Spring Integration and Apache Camel provide a simple and clean approach for the Integration problems in a typical enterprise. They are lightweight frameworks – Spring Integration builds on top of Spring portfolio and extends the familiar programming model for the Integration domain and is easy to pick up, Apache camel provides a good Java based DSL and integrates well with Spring Core, with a fairly gentle learning curve. The article does not recommend one product over the other but encourages the reader to evaluate and learn from both these frameworks.


References:

Spring Integration Website: http://www.springsource.org/spring-integration
Apache Camel Website: http://camel.apache.org/
Spring Integration Reference: http://static.springsource.org/spring-integration/reference/htmlsingle/spring-integration-reference.html
Apache Camel User Guide: http://camel.apache.org/user-guide.html
Plug for my blog: http://biju-allandsundry.blogspot.com/
Spring Framework Spring Integration Apache Camel Enterprise integration

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot Microservices + Apache Camel: A Hello World Example
  • Introducing Stalactite ORM
  • Aggregating REST APIs Calls Using Apache Camel
  • REST Services With Apache Camel

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!