Over a million developers have joined DZone.

Implementing Apache Camel Context and Camel Routes Using Spring Context

· Integration Zone

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and dPaaS could cost you down the road. Brought to you in partnership with Liaison Technologies.

Part 1 – Enabling Camel Context

We can easily start a Camel context in Spring configuration file. Assuming you have a working Spring configured application. Add below code snippet in your Spring context xml.

<CamelContext id = "CamelContext"  xmlns=http://camel.apache.org/schema/spring autostartup ="{$startup}">

 	<!-- Define all camel routes within this context -->


When we start the Spring context, camel context will be automatically enabled. Camel context has a field ‘autoStartup’ which can have value either true or false. This value can be read from a properties file. In order to use above code, we need to add camel namespace in the spring file.

          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

Part 2 – Implementing Camel routes (Camel timer component)

Define all your routes within the camel context. Code snippet added below.

<CamelContext id = "CamelContext"  xmlns=http://camel.apache.org/schema/spring autostartup ="{$startup}">

 	<route id="route_email" startupOrder="1">
 		<from uri="timer://detectir?fixedRate=true&period=12h" />
 		<to uri="bean:emailService?method=sendEmail" />


Here we defined one route called ‘route_email’. This route uses camel timer component which runs at a fixed interval. Above route will run every 12 hours and invoke the method ‘sendEmail’ in ‘EmailService’ class. You need to define ‘EmailService’ class in the Spring xml. Code snippet given below.

<bean id="emailService" class="com.cameltutor.email.EmailService" >
 		<!--  Define properties for this bean if you have any -->
package com.cameltutor.email;

public class EmailService {
  public void sendEmail(){
  // Implement email sending logic within this method

Above description will give a head start to configure Apache Camel with Spring and enable Camel routes.

Discover the unprecedented possibilities and challenges, created by today’s fast paced data climate and why your current integration solution is not enough, brought to you in partnership with Liaison Technologies.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}