Coding Your First Camel Route
A discussion of the architecture behind Camel and how Camel Routes work within large enterprise integration patterns, including sample code to get you started.
Join the DZone community and get the full member experience.
Join For FreeEnterprises consists of N number of systems each supporting different protocols and transports, thereby making enterprise integrations quite complex.
Apache Camel is an open source framework that provides an implementation of various enterprise integration patterns. Camel makes integration easier by providing support for a large variety of transports, data formats, and protocols. There are 100+ components provided for messaging queues, APIs, databases, Kafka, cloud integrations, etc.
Camel Architecture
- Camel Context acts as a runtime for Camel which needs to be started before using the Camel components. Camel Context provides access to all the other components in Camel.
- Route Engine is responsible for managing the routes and can handle N number of routes.
- Components - Different Extensions (JMS, Kafka, etc.).
- Message Based Processors are responsible for transforming and enriching the message.
Creating a Simple Camel Application Using Spring Boot
Go to Spring Initializr and generate a skeleton Spring Boot project by selecting Web and Apache Camel as the dependencies.
Unzip the generated skeleton code and import the project to the IDE. Import the Maven dependencies.
Create a class with the @Component
annotation that extends the RouteBuilder
class and implements the configure()
method to configure the route.
The URI format for the timer component is timer:name[?options]
and we have configured it as ("timer:test?period=5s")
A log is used for intermediate logging.
pollEnrich
does not access any data from the current exchange, which means, when polling, it cannot use any of the existing headers you may have set on the exchange.
The URI format for file components is file://directoryName[?options]
package com.demo.camelspringboot.route;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
public class CamelRoute extends RouteBuilder {
public void configure() throws Exception {
from("timer:test?period=5s")
.log("Timer has been invoked")
.pollEnrich("file:inputdirectory/input?delete=true")
.to("file:outputdirectory/output");
}
}
Create the input directories and create a sample file as shown:
Start the Spring Boot application. Notice that the timer gets invoked every five seconds, as per the configuration.
Notice that the file is deleted from the input directory and copied to the output directory as per the configuration.
Concepts
The exchange object is created when Camel moves the content from the 'from' method to the 'to' method as depicted in the picture below.
- Exchange ID – A unique ID that identifies the exchange.
- MEP (Message Exchange Pattern) – Denotes whether you’re using the InOnly or InOut messaging. The default is InOnly. InOnly represents a one way message and InOut represents a request-response message.
- Properties – Properties are similar to message headers, but they last for the duration of the entire exchange.
- Exception – This will be set in case of any errors during routing.
- Message – The content to be transported.
References
Opinions expressed by DZone contributors are their own.
Comments