【发布时间】:2021-03-10 10:50:32
【问题描述】:
我需要从 sftp 服务器获取修改时间超过给定时间的远程文件名列表,然后从 sftp 中删除所有这些文件,我使用SftpOutboundGateway 的NLST 命令列出文件名字
但是它从给定的远程目录路径获取所有文件,我尝试了 .filter(lastModifiedFileListFilter) 不起作用然后我也尝试了 .filterFunction(logicGiven) 也不起作用
不知NLST命令是否支持过滤
这是代码示例:
过滤器实例:
private FileListFilter lastModifiedFilter(){
LastModifiedFileListFilter fileListFilter = new LastModifiedFileListFilter();
fileListFilter.setAge(120, TimeUnit.SECONDS);
return fileListFilter;
}
使用 filter() 方法:
@Bean
public IntegrationFlow deleteFiles(){
return IntegrationFlows.from("integration.channel.bulk-delete")
.handle(Sftp.outboundGateway(sftpSessionFactory(),
AbstractRemoteFileOutboundGateway.Command.NLST,"headers[path]")
.filter(lastModifiedFilter()))
.get();
使用 filterFunction() 方法:
@Bean
public IntegrationFlow deleteFiles(){
return IntegrationFlows.from("integration.channel.bulk-delete")
.handle(Sftp.outboundGateway(sftpSessionFactory(),
AbstractRemoteFileOutboundGateway.Command.NLST,"headers[path]")
.filterFunction(file->{
Instant decidedTime = Instant.now().minus(120, ChronoUnit.SECONDS);
return Instant.ofEpochSecond(file.getAttrs().getMTime()).isBefore(decidedTime);
}))
.log()
.get();
如何过滤和获取文件名。我不想使用 mget 命令
更新
下一个句柄方法中的 RM 命令不会删除,我还在 doRm() 方法中放置了一个断点,尽管我的 for 循环中有文件,但它并没有停在那里
@Bean
public IntegrationFlow deleteFiles(){
return IntegrationFlows.from("integration.channel.bulk-delete")
.handle(Sftp.outboundGateway(sftpSessionFactory(),
AbstractRemoteFileOutboundGateway.Command.LS,"headers[path]")
.options(AbstractRemoteFileOutboundGateway.Option.NAME_ONLY)
.filterFunction(file->{
Instant decidedTime = Instant.now().minus(120, ChronoUnit.SECONDS);
return Instant.ofEpochSecond(file.getAttrs().getMTime()).isBefore(decidedTime);
}))
.log()
.handle(fileList->{
List<String> files = (List<String>) fileList.getPayload();
for (String remoteFile: files) {
Sftp.outboundGateway(sftpSessionFactory(),
AbstractRemoteFileOutboundGateway.Command.RM,
"headers[file_remoteDirectory]+"+remoteFile);
}
})
.get();
【问题讨论】:
标签: spring-integration spring-integration-dsl spring-integration-sftp