Camel spring boot simple standalone folder read and write example

In this post, we will discuss how to move a file from one folder to another using Apache Camel and Spring Boot.

POM configurations:

camel-spring-boot-starter contains all the basic jars required for this example.
Add the following dependencies in the pom.

<dependencyManagement>
    <dependencies>
|       <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-spring-boot-bom</artifactId>
            <version>3.7.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

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

Main class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Route:

Create a route to move file from one folder to another.

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

@Component
public class DemoRoute extends RouteBuilder 
{
    @Override
    public void configure() throws Exception
    {
        from( “file://F://work//test//in?delete=true” )
        .to( “log:com.codebonneamie.demo.DemoRoute” )
            .to( “file://F://work//test//out” );
    }
}

If you run the application now, it will get stopped immediately after starting without moving any files.
To keep the JVM running, set the following property as JVM arguments.
Run the application again, and all the files from the first folder gets transferred to the second folder.

camel.springboot.main-run-controller=true

Output:

2021-01-26 19:33:57.375 INFO 11568 — [/work//test//in] com.codebonneamie.demo.DemoRoute : Exchange[ExchangePattern: InOnly, BodyType: org.apache.camel.component.file.GenericFile, Body: [Body is file based: GenericFile[F:\work\test\in\abc.txt]]]

Leave a Comment

Your email address will not be published. Required fields are marked *