Over a million developers have joined DZone.

Implementing Apache Camel Context and Camel Routes Using Spring Context

DZone 's Guide to

Implementing Apache Camel Context and Camel Routes Using Spring Context

· Integration Zone ·
Free Resource

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.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}