0
votes

Inbound channel adapter is created with a poller to poll files present in root directory and its sub directories

e.g.  

RootDir 
|_abc.txt
|_subdirectory1
  |_subdirfile1.doc

The problem is inbound channel adapter is reading the directory also as message

@Bean
@InboundChannelAdapter(autoStartup = "false", value = "incomingchannel", poller = @Poller("custompoller"))
    public MessageSource<File> fileReadingMessageSource(DirectoryScanner directoryScanner) {

    FileReadingMessageSource sourceReader = new FileReadingMessageSource();
sourceReader.setScanner(directoryScanner);

}

@Bean
    public DirectoryScanner directoryScanner() {
        DirectoryScanner scanner = new RecursiveDirectoryScanner();
        CompositeFileListFilter filter = new CompositeFileListFilter<>(
                Arrays.asList(new AcceptOnceFileListFilter<>(), new RegexPatternFileListFilter(regex)));
        scanner.setFilter(filter);
        return scanner;
    }

@Trasnformer(inputChannel="incomingchannel",....
torequest(Mesage<File> message) { 

        message.getPayload()

}

Here message.getpayLoad is printing subdirectory1 i.e. directory is also read as a file message

I can handle explicitly as file is directory or not in trasnformer and ignore, but wanted to know is there any way it can be filtered in Recursive Directory scanner attached to Inbound Channel adapter ?

1

1 Answers

1
votes

This problem is probably related to this SO thread: Spring Integration + file reading message source _ Inbound Channel Adapter + Outbound Gateway.

You need to think twice if you are OK loosing file tree. It sounded for me that you would like to restore a tree in the FileWritingMessageHandler. So, it is probably better to @Filter messages with directory payload before sending to that transformer.

If you still want to skip dirs from the producing, consider to use a ChainFileListFilter instead of CompositeFileListFilter and configure a RegexPatternFileListFilter first. This way a filtered directory from the RegexPatternFileListFilter (it is skipped by default see AbstractDirectoryAwareFileListFilter) won't go to the AcceptOnceFileListFilter at all. In your current configuration the AcceptOnceFileListFilter being first accepts a directory and really ignores the next filter in the composition.

UPDATE

What I mean should be like this:

@Bean
public DirectoryScanner directoryScanner() {
    DirectoryScanner scanner = new RecursiveDirectoryScanner();
    ChainFileListFilter filter = new ChainFileListFilter<>(
            Arrays.asList(new RegexPatternFileListFilter(regex), new AcceptOnceFileListFilter<>()));
    scanner.setFilter(filter);
    return scanner;
}

Nothing more. As long as your regex is just for files, any sub-directory would be skipped and not allowed to go downstream.