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 Free
Enterprises 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 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
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
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.
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.
Opinions expressed by DZone contributors are their own.