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:


The starting point is FileReadDirectoryApplication:

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.

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:


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:


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:


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.

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.

Labels: ,