Over a million developers have joined DZone.

Spring Boot and Apache Camel

DZone 's Guide to

Spring Boot and Apache Camel

We take a look at the Apache Camel integration tool, and how it can be used with Spring Boot in your Java-based coding projects.

Free Resource

As the world of software moves on, more complex systems are being developed, which have to integrate with each other. It started with SOA and it continues with microservices.

Camel is the number one integration tool that comes to my mind, since, nowadays, Spring Boot with Camel is a very strong combination.

The first step is to include the Camel dependencies to our Spring project.

buildscript {
    ext {
        springBootVersion = '1.5.9.BUILD-SNAPSHOT'
    repositories {
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    dependencies {
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.gkatzioura'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
dependencies {

In order to have a faster project setup from scratch, you can always use the online Spring initializer.

Now let's add a simple route:

package com.gkatzioura.springcamel.routes;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

public class TimerRoute extends RouteBuilder {

    public static final String ROUTE_NAME = "TIMER_ROUTE";

    public void configure() throws Exception {

We don't have to worry about the Camel context configuration since the Camel auto-configuration creates a SpringCamelContext for you and takes care of the proper initialization and shutdown of that context.

Also, Camel auto-configuration collects all the RouteBuilder instances from the Spring context and automatically injects them into the provided CamelContext. Thus we don't have to register our routes to the CamelContext.

As you can see, our route has a timer with a period of 10000 milliseconds which routes to a log endpoint. The log endpoint will print the executed string every 10000 milliseconds.

Keep in mind that if no routeId is specified, Camel will assign a name on its own, therefore giving a name to our route definition is a good practice in case we want to retrieve the root definition.

In order for Camel to stay up, we need to keep our main thread blocked. Thus, we add this configuration to our application.yml file.

    main-run-controller: true

Instead of this, we can include the spring-boot-starter-web dependency, but our application has as few dependencies as possible, and we need to keep it this way.

However, the most difficult part of the integration with other systems is testing. Throughout the years, there have been rapid advancements in testing and the tools that we use.
Camel also comes packaged with some great tools in order to unit test.

For example, we will implement a test of the route specified previously.

public class SpringCamelApplicationTests {

    @EndpointInject(uri = MOCK_RESULT)
    private MockEndpoint resultEndpoint;

    private CamelContext camelContext;

    @EndpointInject(uri = MOCK_TIMER)
    private ProducerTemplate producer;

    private static final String MOCK_RESULT = "mock:result";
    private static final String MOCK_TIMER = "direct:mock-timer";

public void setup() throws Exception {

                .adviceWith(camelContext, new AdviceWithRouteBuilder() {
                    public void configure() throws Exception {

    public void sendMessage() throws Exception {

        producer.sendBody("A message");


Let's have a look at each part of the test.

Our JUnit runner of choice would be the CamelSpringBootRunner.class


We inject a ProducerTemplate. The ProducerTemplate interface allows you to send message exchanges to endpoints in a variety of different ways to make it easy to work with Camel Endpoint instances from Java code.

Then we inject a MockEndpoint. The MockEndpoint will serve us by replacing the original endpoint. Then we will set the expected number of messages to be received. Once the processing is done, we assert that the amount of received messages is satisfied.

On our setup method, we will replace our original endpoint with the fake producer template endpoint. Thus, our route will receive the events that we will issue from the ProducerTemplate.
Then we will also intercept the log endpoint and direct the message to the MockEndpoint previously specified.

So we ended up with a camel application and a unit test for the route specified.
You can find the source code on GitHub.

integartion ,apache camel ,spring boot ,application integration

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}