Spring 集成文件同步器:根据预定义列表接受文件

Spring Integration File synchronizer : Accept files based on a pre defined list

我正在通过Sftp 将文件从远程传输到本地,以进行处理。 我只想传输 .csv 文件,并且我有一个预定义文件名列表。

我找不到允许指定多个模式并在至少一个匹配时传输的 FileListFilter。

到目前为止我有这段代码,它正在为“.csv”过滤工作。

集成流程

@Bean
    public IntegrationFlow integFlow() {
        return IntegrationFlows
            .from(ftpMessageSource(), c -> poller())
            ... more processing

消息来源

public MessageSource<File> ftpMessageSource() {

            SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sessionFactory);
            fileSynchronizer.setRemoteDirectory(remoteDirectory);
            fileSynchronizer.setDeleteRemoteFiles(true);
            fileSynchronizer.setFilter(new SftpRegexPatternFileListFilter(Constantes.EXTENSION));
            SftpInboundFileSynchronizingMessageSource ftpInboundFileSync = 
                new SftpInboundFileSynchronizingMessageSource(fileSynchronizer);

            ftpInboundFileSync.setLocalDirectory(new File(workDirectory));
            ftpInboundFileSync.setAutoCreateLocalDirectory(true);
            CompositeFileListFilter<File> compositeFileListFilter = new CompositeFileListFilter<>();
            compositeFileListFilter.addFilter(new RegexPatternFileListFilter(Constantes.EXTENSION));
            ftpInboundFileSync.setLocalFilter(compositeFileListFilter);
            return ftpInboundFileSync;
    }

Constantes.EXTENSION 是接受 .csv 和 .CSV 的正则表达式。这很好用。

假设我有一个包含 "string1',"string2","string3" 的字符串列表,我想传输 string1*、string2* 或 string3* 形式的每个文件。我将如何进行?

有个CompositeFileListFilter:

* Simple {@link FileListFilter} that predicates its matches against <b>all</b> of the
* configured {@link FileListFilter}.

逻辑如下:

public boolean accept(F file) {
    AtomicBoolean allAccept = new AtomicBoolean(true);
    // we can't use stream().allMatch() because we have to call all filters for this filter's contract
    this.fileFilters.forEach(f -> allAccept.compareAndSet(true, f.accept(file)));
    return allAccept.get();
}

因此,您将此 CompositeFileListFilter 配置为多个 SftpRegexPatternFileListFilter 代表,只要它们至少超过 CompositeFileListFilter 中的过滤器之一,您的文件就会被处理。

在文档中查看有关过滤器的更多信息:https://docs.spring.io/spring-integration/docs/current/reference/html/file.html#file-reading

@SpringBootApplication
public class So59161698Application {

    public static void main(String[] args) {
        SpringApplication.run(So59161698Application.class, args);
    }

    private final String myPatterns = "foo,bar,baz";

    @Bean
    public FileListFilter<File> filter() {
        Set<String> patterns = StringUtils.commaDelimitedListToSet(this.myPatterns);
        return files -> Arrays.stream(files)
                .filter(file -> patterns.stream()
                        .filter(pattern -> file.getName().startsWith(pattern))
                        .findFirst()
                        .isPresent())
                .collect(Collectors.toList());
    }

    @Bean
    public ApplicationRunner runner(FileListFilter<File> filter) {
        return args -> {
            System.out.println(filter.filterFiles(new File[] {
                    new File("foo.csv"),
                    new File("bar.csv"),
                    new File("baz.csv"),
                    new File("qux.csv")
            }));
        };
    }

}
[foo.csv, bar.csv, baz.csv]