SFTP 集成 - SftpInboundFileSynchronizer - 如何不再下载相同的文件

SFTP Integration - SftpInboundFileSynchronizer - How not to download same file again

在我当前的应用程序中,使用 spring-批处理作业,我触发了将 SFTP 远程文件传输到本地目录的过程,对其进行处理并删除文件 post 处理。


    @Bean("ftpMessageSource")
    @EndpointId("streamInboundAdapter")
    @InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "5000"), autoStartup = "false")
    public MessageSource<File> sftpMessageSource() {
        SftpInboundFileSynchronizingMessageSource source =
                new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
        source.setLocalDirectory(new File("sftp-inbound"));
        source.setAutoCreateLocalDirectory(true);
        source.setLocalFilter(new FileSystemPersistentAcceptOnceFileListFilter(metadataStore(),""));
        source.setMaxFetchSize(10);
        return source;
    }


    @Bean(name="fileStore")
    public PropertiesPersistingMetadataStore metadataStore() {
        PropertiesPersistingMetadataStore metadataStore = new PropertiesPersistingMetadataStore();
        metadataStore.setBaseDirectory("filestore");
        metadataStore.setFileName("filestore.properties");
        metadataStore.afterPropertiesSet();
        return metadataStore;
    }

在处理每个文件时,我将文件名输入到 fileStore.properties 文件中。

    
metadataStore.put(file.getName(),file.getName());


一个问题,我 运行 进入下一次处理(w/o 重新启动服务器并再次启动相同的进程)是 - 进程再次获取相同的文件集进行处理。

我不想用 SFTP 处理文件,请您指出我缺少哪个配置以避免再次下载相同的文件。

and delete files post processing.

所以,文件不再存在于本地目录中。由于您不过滤远程文件,它们将作为新的本地副本再次下载。

FileSystemPersistentAcceptOnceFileListFilter 逻辑基于 file.lastModified(),如果它与现有条目不同,它将被替换并因此推向下游。

考虑在 sftpInboundFileSynchronizer 上使用 SftpPersistentAcceptOnceFileListFilter。这样就不会从 SFTP 中提取相同的文件(如果相同 lastModified)。

在文档中查看更多内容:https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/sftp.html#sftp-inbound