Search code examples
javaspring-bootjaxbspring-integrationfile-watcher

How to use File Watcher from Spring Integration only for Waiting, NOT for Moving?


I'm learning about Spring Integration in a Spring Boot app, and I need to make an app to wait for .txt files in a folder, and then do some processing, and then put the resulting .txt files in another folder.

So the app should wait for inputxxx.txt files in the input folder, then do some processing, and then put the resulting outputxxx.txt files into output folder. The inputxxx.txt files should remain in the input folder and in the output folder should be only the outputxxx.txt files.

@Configuration
@EnableIntegration
public class ApplicationConfig {

    @Bean
    public MessageChannel fileChannel() {
        return new DirectChannel();
    }

    @Bean
    @InboundChannelAdapter(value = "fileChannel", poller = @Poller(fixedDelay = "1000"))
    public MessageSource<File> fileReadingMessageSource() {

        SimplePatternFileListFilter filter = new SimplePatternFileListFilter(FILE_PATTERN);
        return new FilePoller(directory, filter);
    }

    @Bean
    @ServiceActivator(inputChannel = "fileChannel")
    public MessageHandler fileWritingMessageHandler() {
        return new FileHandler(OUTPUT_LOCATION);
    }
}

FilePoller class:

public class FilePoller extends FileReadingMessageSource {

    public FilePoller(File directory, FileListFilter<File> filter) {
        super.setDirectory(directory);
        super.setFilter(filter);
    }
}

So I don't want to use the move() method, because JAXB will do what I want, but if I delete move() my app will not stop.

Thank you! Any feedback will be apreciated!


Solution

  • Your problem is here:

        SimplePatternFileListFilter filter = new SimplePatternFileListFilter(FILE_PATTERN);
        return new FilePoller(directory, filter);
    

    You just check for the particular file type, but you don't guard your program for picking up the same file on the next polling cycle.

    Please, consider to use a ChainFileListFilter where this pattern one would be first, but the second one must be AcceptOnceFileListFilter.

    This way you fully can ignore an original file and just concentrate on your output file. The original file is not going to be picked again on the next polling cycle.

    For permanent ignorance between program restart you should consider using a FileSystemPersistentAcceptOnceFileListFilter.

    See more in Docs:

    https://docs.spring.io/spring-integration/docs/current/reference/html/file.html#file-reading