In this tutorial you will learn how to bootstrap Camel in a Spring Boot Application in a matter of minutes!

First of all, let's create our project from a basic Web template:

spring init -dweb spring-camel

Ok, we have included the web starter as part of our kit, we will now add Apache Camel dependencies so that we can start firing routes. Include in your pom.xml camel dependencies as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/>  
	</parent>
	<groupId>com.example</groupId>
	<artifactId>spring-camel</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

               <dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-spring-boot-starter</artifactId>
			<version>2.17.0</version>
              </dependency>
 
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

In order to trigger the Camel wheel, we need to inject the CamelContext in our application. To make it really simple, we will inject it into the main class:

package com.example.springcamel;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.apache.camel.CamelContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.camel.builder.RouteBuilder;

@SpringBootApplication
public class DemoApplication {

	@Autowired
	CamelContext camelContext;

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}


    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {

       return args -> {

            camelContext.addRoutes(new RouteBuilder() {
 
             public void configure() {
               from("file:/home/frank/test/in?noop=true").to("file:/home/frank/test/out");
            } 
 
         });

        };


    }
}

As you can see, we have added a new Camel route using the fluent syntax of the RouteBuilder class. This simple Camel route will copy files from a source folder to a destination folder. Make sure you have the path of the file Component available on your machine.

Now run the application in any of the possible ways, such as:

java -jar target/spring-camel-0.0.1-SNAPSHOT.jar

You will see from the output that the Camel route has been bootstrapped and executed:

2018-12-11 10:44:31.248  INFO 12432 --- [       Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2018-12-11 10:44:31.249  INFO 12432 --- [       Thread-3] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.17.0 (CamelContext: camel-1) is shutting down
2018-12-11 10:44:31.250  INFO 12432 --- [       Thread-3] o.a.camel.impl.DefaultShutdownStrategy   : Starting to graceful shutdown 1 routes (timeout 300 seconds)
2018-12-11 10:44:31.254  INFO 12432 --- [ - ShutdownTask] o.a.camel.impl.DefaultShutdownStrategy   : Route: route1 shutdown complete, was consuming from: Endpoint[file:///home/frank/test/in?noop=true]
2018-12-11 10:44:31.254  INFO 12432 --- [       Thread-3] o.a.camel.impl.DefaultShutdownStrategy   : Graceful shutdown of 1 routes completed in 0 seconds

As a proof of concept, check the destination folder to see if files have been copied from the source.

Starting a Camel Route from a Spring REST Service

If you want to start a Camel Route upon a request, the simplest way is by means of a REST Service. Just plug one in:

package com.example.springcamel;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CamelController {
	
	@Autowired
	CamelContext camelContext;
	
	@Autowired
	ProducerTemplate producerTemplate;

	@RequestMapping(value = "/camel")
	public void startCamel() {
                System.out.println("Received call!");  
		producerTemplate.sendBody("direct:demoRoute", "Calling via Spring Boot Rest Controller");
	}
	
 
}

Now we need a consumer for that route, so just add one:

package com.example.springcamel;

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

@Component
public class CamelRoute extends RouteBuilder{

	@Override
	public void configure() throws Exception {
		from("direct:demoRoute")
			.log("Got message: ${body}");
	}

}

Now reach the "/camel" endpoint from the browser or shell:

curl http://localhost:8080/camel

You will see that in your Spring Boot Console, the log message is printed:

Camel body: Calling via Spring Boot Rest Controller
FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials