Introduction

We will create an example to read file content from source directory using file adapter in Spring integration. This example will show you how to read file content from a particular location and log the file content into console. You may further process this file content for your business requirements.

Prerequisites

Eclipse 4.12, Java 8 or 12, Gradle 5.6, Spring Boot 2.1.8

Creating Project

Create a gradle based project in Eclipse with the project’s name as spring-integration-file.

Updating Build Script

The default generated build.gradle script has to be updated to include the required dependencies.

We have two dependencies for Spring integration and file adapter in the below build script.

buildscript {
	ext {
		springBootVersion = '2.1.7.RELEASE'
	}
	
    repositories {
    	mavenLocal()
    	mavenCentral()
    }
    
    dependencies {
    	classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

sourceCompatibility = 12
targetCompatibility = 12

repositories {
	mavenLocal()
    mavenCentral()
}

dependencies {
	implementation("org.springframework.boot:spring-boot-starter-integration:${springBootVersion}")
	implementation("org.springframework.integration:spring-integration-file:5.1.7.RELEASE")
}

Creating Application Properties

We will create an application.properties file under src/main/resources which will content key/value pair properties.

For example, we will put input directory name from where file or files will be read.

file.input.directory=C:/file-input

The above properties file may be updated as and when required for the below examples.

Creating Config Class

We will create a config class that contains several configurations for the applications, such as, creation of Spring beans, reading properties file etc.

package com.jeejava.spring.integration.file.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.integration.config.EnableIntegration;

@Configuration
@EnableIntegration
public class FileConfig {

	@Autowired
	private Environment env;
	
	// ...

}

The above class will be updated as and when required for the example shown below.

Creating Main Class

Main class with @SpringBootApplication is enough to deploy the application into embedded Tomcat server.

We will run the application using CommandLineRunner.

package com.jeejava.spring.integration.file;

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

@SpringBootApplication
public class SpringIntegrationFileApp implements CommandLineRunner {

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

	@Override
	public void run(String... args) throws Exception {
		//...
	}

}

The above class will be updated as and when required for the example shown below.

Reading File From Directory

We will create an example where we will read a text file from a particular directory and read the file content and log to the console.

Creating FileReadingMessageSource Bean

We need to create a FileReadingMessageSource bean in order to read the file from a source directory.

We will configure the source directory into this bean to read the file from the source directory.

Put the below code snippets into the FileConfig class.

@Bean
public FileReadingMessageSource fileReadingMessageSource() {
	FileReadingMessageSource fileReadingMessageSource = new FileReadingMessageSource();
	fileReadingMessageSource.setDirectory(new File(env.getProperty("file.input.directory")));
	return fileReadingMessageSource;
}

Reading File Content

Now we will update our main class to read the file content as a string value.

@Autowired
private FileReadingMessageSource fileReadingMessageSource;

@Override
public void run(String... args) throws Exception {
	File file = fileReadingMessageSource.receive().getPayload();
	String content = Files.readString(Paths.get(file.getPath()));
	System.out.println(content);
}

Testing the Application

Run the main class and you will see below output in the console:

Hi,
This is a sample text file

Sample text file: Download

Thanks for reading.

Tags:

Leave a Reply

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