Spring Integration – Polling file creation and modification

1 Introduction

File support is another of Spring Integration’s endpoints to communicate with external systems. In this case, it provides several components to read, write and transform files. During this post, we are going to write an application which monitors a directory in order to read all files in there. In concrete it does the following:

The source code can be found in Github.

2 Configuration

The application is built with Spring Boot, since it eases configuration significantly. To create the initial infrastructure of the application, you can go to https://start.spring.io/, select the Integration module and generate the project. Then you can open the zip file in your favourite IDE.

I added a couple of dependencies to the pom.xml like commons.io or Spring Integration Java DSL. My pom.xml file looks like 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">
  <description>Demo project for Spring Boot</description>
    <relativePath/> <!-- lookup parent from repository -->
    <!-- Spring Integration - Java DSL -->

The starting point is FileReadDirectoryApplication:

public class FileReadDirectoryApplication {
    public static void main(String[] args) throws IOException, InterruptedException {
		SpringApplication.run(FileReadDirectoryApplication.class, args);

Starting from here, we are going to add the Spring Integration components for reading from a specific folder of the filesystem.

3 Adding the adapter

In order to read from the file system, we need an inbound channel adapter. The adapter is a file reading message source, which is responsible of polling the file system directory for files and create a message from each file it finds.

@InboundChannelAdapter(value = "fileInputChannel", poller = @Poller(fixedDelay = "1000"))
public MessageSource<File> fileReadingMessageSource() {
    CompositeFileListFilter<File> filters = new CompositeFileListFilter<>();
    filters.addFilter(new SimplePatternFileListFilter("*.txt"));
    filters.addFilter(new LastModifiedFileFilter());
    FileReadingMessageSource source = new FileReadingMessageSource();
    source.setDirectory(new File(DIRECTORY));
    return source;

We can prevent some types of files from being polled by setting a list of filters to the message source. For this example two filters have been included:

4 Processing the files

For each polled file, we will transform its content to String before passing it to the processor. For this purpose, Spring already provides a component:

public FileToStringTransformer fileToStringTransformer() {
    return new FileToStringTransformer();

Hence, instead of receiving a Message<File>, the processor will receive a Message<String>. The file processor is our custom component which will do something as advanced as printing the file content:

public class FileProcessor {
    private static final String HEADER_FILE_NAME = "file_name";
    private static final String MSG = "%s received. Content: %s";
    public void process(Message<String> msg) {
        String fileName = (String) msg.getHeaders().get(HEADER_FILE_NAME);
        String content = msg.getPayload();
        System.out.println(String.format(MSG, fileName, content));

5 Building the flow

Now that we have all the required components in place, let’s build the flow. We are using Spring Integration Java DSL, since it makes the flow more readable:

public IntegrationFlow processFileFlow() {
    return IntegrationFlows
        .handle("fileProcessor", "process").get();
    public MessageChannel fileInputChannel() {
        return new DirectChannel();

6 Running the application

In my directory, I already have a file called ‘previousFile.txt’. After starting the application, we will create two files and modify one of them.

public static void main(String[] args) throws IOException, InterruptedException {
    SpringApplication.run(FileReadDirectoryApplication.class, args);
private static void createFiles() throws IOException, InterruptedException {
    createFile("file1.txt", "content");
    createFile("file2.txt", "another file");
    appendFile("file1.txt", " modified");

If we run the application, we should see the following print statements:

previousFile.txt received. Content: previous content
file1.txt received. Content: content
file2.txt received. Content: another file
file1.txt received. Content: content modified

7 Conclusion

This example shows how simple it is to read files from a directory using Spring Integration, obviously with the help of Spring Boot to simplify the configuration. Depending on your needs, you can add your own custom filters to the message source, or use another one of the provided by Spring, like the RegexPatternFileListFilter. You can check for other implementations here.

If you found this post useful, please share it or star my repository :)

I’m publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.

Exit mobile version