0

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

rkosegi
  • 14,165
  • 5
  • 50
  • 83
Samatha
  • 5
  • 1

1 Answers1

0

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.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thanks for the response , but how do we call public File[] listFiles(){ }, I am new to spring . can you please share me an sample or example for it – Samatha Apr 01 '22 at 09:08
  • I'm not sure what you see as Spring'y in just plain `java.io.File.listFiles()`. See a general Java tutorial: https://www.baeldung.com/java-list-directory-files – Artem Bilan Apr 01 '22 at 13:30