javafilespring-integrationfile-watcher

Spring integration file watcher


I am using spring integration for file watcher and I am successfully able to poll file in the specified time. But I want to look into the specified directory and get the list of files and process them rather than processing individual files .below is the code snippet

@Bean
public IntegrationFlow processFileFlow() {
    return IntegrationFlows
    .from("fileInputChannel")
    .handle("fileProcessor", "process").get();
}

@Bean
public MessageChannel fileInputChannel() {
    return new DirectChannel();
}
public class FileProcessor {

    public void process(List<File> files) { // I want this method to accept list of files
        //here I want to get list of files but I am always receiving individual files only 
    }

}

I tried by adding FileListFilter which will return list of file , but process() is not picking list of files as arguments , but only individual file is coming


Solution

  • If your goal is to have a directory as an input and do file listing yourself, then you must not look into the FileReadingMessageSource and its WatchService feature.

    Your process() may just receive a File object as an input dir, then you call its public File[] listFiles() { and do whatever you need according to your logic.

    You also may use java.nio.file.Files features for your task.

    I mean there is nothing sophisticated with just listing the files from dir to justify the fully-blown Spring Integration component.