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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  1. DZone
  2. Coding
  3. Frameworks
  4. Mediation Routing Using Apache Camel

Mediation Routing Using Apache Camel

Michał Warecki user avatar by
Michał Warecki
·
Sep. 13, 12 · Interview
Like (0)
Save
Tweet
Share
8.08K Views

Join the DZone community and get the full member experience.

Join For Free
In this part of the article we proceed to the practical side of integration using patterns. As implementation of EIPs I’ll use Apache Camel integration platform.
The basic unit of work in Apache Camel is route. Route is simply a flow of EIPs, where an output of one EIP is an input of the other. So let's start with a simple example.
I would like to sort the xml files based on their content. When xml element denoting person’s city has value ‘London’ then file needs to be copied to messages/uk directory, in other case to messages/others directory. Additionally I want to log each copy operation. Here is EIPs flow implementing described above logic:
 

Below you can see the configuration of Apache Camel context:

1:  <code style="color: black; word-wrap: normal;">1:  <camelContext xmlns="http://camel.apache.org/schema/spring">  
2:    <route>  
3:      <from uri="file:src/data?noop=true"/>  
4:      <choice>  
5:        <when>  
6:          <xpath>/person/city = 'London'</xpath>  
7:          <log message="UK message"/>  
8:          <to uri="file:target/messages/uk"/>  
9:        </when>  
10:        <otherwise>  
11:          <log message="Other message"/>  
12:          <to uri="file:target/messages/others"/>  
13:        </otherwise>  
14:      </choice>  
15:    </route>  
16:  </camelContext>  </code><code style="color: black; word-wrap: normal;">
</code>

As the name suggests, from is a declaration of source endpoint that listens to events / polls events. In this example, endpoint is a file poller. On the website http://camel.apache.org/components.html you can find list of all available Apache Camel components, each one with detailed description of uri configuration.
The next element in the configuration is choice when otherwise, which is content based router. In our example when condition is an xpath expression /person/city = 'London'. Node otherwise is chosen when none of the when conditions are fulfilled. To log messages you can use log element. The last node is to which is target endpoint.

XML on the left is copied to messages/uk, and XML on the right is copied to messages/others.

 <person user="james">  
  <firstName>James</firstName>  
  <lastName>Strachan</lastName>         
  <city>London</city>  
 </person>  
 <person user="michal">  
  <firstName>Michał</firstName>         
  <lastName>Warecki</lastName>  
  <city>Warszawa</city>  
 </person>  

The next section will show how to run aforementioned example.
Apache Camel

Published at DZone with permission of Michał Warecki, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Automated Performance Testing With ArgoCD and Iter8
  • How and Why You Should Start Automating DevOps
  • Connecting Your Devs' Work to the Business
  • Easy Smart Contract Debugging With Truffle’s Console.log

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: