在我当前的应用程序中,使用spring-batch作业,我触发了将SFTP远程文件发送到本地目录的过程,对其进行处理并在后期处理后删除文件。
@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());
我遇到的下一个问题是下次处理(不重新启动服务器并再次启动相同的进程)是-进程再次获取同一组文件进行处理。
我不想使用SFTP处理的文件,能否请您指出我缺少的配置,以避免再次下载相同的文件。
并删除文件后处理。
因此,文件不再存在于本地目录中。由于您不过滤远程文件,因此它们将作为新的本地副本再次下载。
该FileSystemPersistentAcceptOnceFileListFilter
逻辑基于file.lastModified()
,如果与现有条目不同,则将其替换并因此推入下游。
考虑到使用SftpPersistentAcceptOnceFileListFilter
的sftpInboundFileSynchronizer
。这样,lastModified
就不会从SFTP提取相同的文件(如果相同)。
在docs中查看更多信息:https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/sftp.html#sftp-inbound
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句