Automate File Classification with Apache Camel



We usually download several files to the Downloads folder, you might find various files in diverse formats like MP3, PDF, docs, srt , MP4 etc.Then you start moving the files from download folder to other folders, which can be very tiresome and monotonous task.

In this post i am gonna show how to shift all your files from Downloads folder to respective folder, by using a simple Apache Camel route.

Apache Camel is a rule-based routing and mediation engine that provides a Java object-based implementation of the Enterprise Integration Patterns using an API (or declarative Java Domain Specific Language) to configure routing and mediation rules.

Lets first create a simple Spring Boot maven project

Create a maven project and include spring Boot and Apache Camel dependencies.



<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>
<groupId>com.file.organizer</groupId>
<artifactId>file_organizer_camel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.19.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
view raw pom.xml hosted with ❤ by GitHub

Now create the Spring Boot main file as

package com.filetransfer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootMain {
public static void main(String[] args) {
SpringApplication.run(SpringBootMain.class, args);
}
}
Next, we add a class with the Camel routes.This routes will be started automatically.

package com.filetransfer;
import org.apache.camel.Predicate;
import org.apache.camel.builder.PredicateBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class SimpleRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
Predicate predicate = PredicateBuilder.and(simple("${file:name.ext} == 'pdf'"));
Predicate predicate1 = PredicateBuilder.and(simple("${file:name.ext} == 'txt'"));
Predicate predicate2 = PredicateBuilder.and(simple("${file:name.ext} == 'mp4'"));
Predicate predicate3 = PredicateBuilder.and(simple("${file:name.ext} == 'mp3'"));
from("file:C://downloads?noop=true").choice()
.when(predicate).to("file:D://Documents");
.when(predicate1).to("file:D://Text_Files");
.when(predicate2).to("file:D://Vidoes");
.when(predicate3).to("file:D://Music");
}
}

To keep the main thread blocked so that Camel stays up, add camel.springboot.main-run-controller=true to the application.properties

camel.springboot.main-run-controller=true
Run this Program in ide or in a console and you will see all your files are sorted.

Hope you liked the post!

Comments

Popular posts from this blog

Interfaces just got much better in Java 8