Tan*_*ain 2 java spring spring-integration spring-integration-sftp
根据此文档,找不到从远程 SFTP 重新下载本地删除文件的正确过程。
要求是删除已经从远程 SFTP 获取的本地文件,并在需要时使用 sftp-inbound-adapter(DSL 配置)重新获取相同的文件。在此实现,MetadataStore没有持续到任何外部系统,如PropertiesPersistingMetadataStore或Redis的元数据存储。因此,根据doc,MetadataStore存储在In-Memory 中。
找不到任何方法来删除该远程文件的元数据,MetadataStore以使用file_name. 并且没有任何线索,应该如何removeRemoteFileMetadata()实现这个回调(根据这个文档)。
配置类包含以下内容:
@Bean
public IntegrationFlow fileFlow() {
SftpInboundChannelAdapterSpec spec = Sftp.inboundAdapter(sftpConfig.getSftpSessionFactory())
.preserveTimestamp(true)
.patternFilter(Constants.FILE_NAME_CONVENTION)
.remoteDirectory(sftpConfig.getSourceLocation())
.autoCreateLocalDirectory(true)
.deleteRemoteFiles(false)
.localDirectory(new File(sftpConfig.getDestinationLocation()));
return IntegrationFlows
.from(spec, e -> e.id("sftpInboundAdapter").autoStartup(false)
.poller(Pollers.fixedDelay(5000).get()))
.channel(MessageChannels.direct().get())
.handle(message -> {
log.info("Fetching File : " + message.getHeaders().get("file_name").toString());
})
.get();
}
Run Code Online (Sandbox Code Playgroud)
我试图解决这个问题,并使用了Tanvir Hossain的参考代码。我是这样编码的。
@Bean
public IntegrationFlow fileFlow() {
SftpInboundChannelAdapterSpec spec = Sftp
.inboundAdapter(sftpConfig.getSftpSessionFactory())
.preserveTimestamp(true)
.filter(sftpFileListFilter())
.localFilter(systemFileListFilter())
.remoteDirectory(sftpConfig.getSourceLocation())
.autoCreateLocalDirectory(true)
.deleteRemoteFiles(false)
.localDirectory(new File(sftpConfig.getDestinationLocation()));
return IntegrationFlows
.from(spec, e -> e.id("sftpInboundAdapter").autoStartup(false)
.poller(Pollers.fixedDelay(5000).get()))
.channel(MessageChannels.direct().get())
.handle(message -> {
log.info("Fetching File : "
+ message.getHeaders().get("file_name").toString());
})
.get();
}
private FileSystemPersistentAcceptOnceFileListFilter systemFileListFilter() {
return new FileSystemPersistentAcceptOnceFileListFilter(store(), prefix);
}
private ChainFileListFilter<ChannelSftp.LsEntry> sftpFileListFilter() {
ChainFileListFilter<ChannelSftp.LsEntry> chainFileListFilter =
new ChainFileListFilter<>();
chainFileListFilter.addFilters(
new SftpPersistentAcceptOnceFileListFilter(store(), prefix),
new SftpSimplePatternFileListFilter(sftpConfig.getFileFilterValue())
);
return chainFileListFilter;
}
@Bean
public SimpleMetadataStore store() {
return new SimpleMetadataStore();
}
Run Code Online (Sandbox Code Playgroud)
我Controller的删除元数据如下所示:
public class Controller {
private final SimpleMetadataStore simpleMetadataStore;
public Controller(SimpleMetadataStore simpleMetadataStore) {
this.simpleMetadataStore = simpleMetadataStore;
}
@GetMapping("/test/remove-metadata/{type}/{fileName}")
@ResponseBody
public String removeFileMetadata(
@PathVariable("fileName") String fileName,
@PathVariable("type") String type
) {
String prefix = definedPrefix;
String filePath = "";
if(type.equals("local")){
filePath = "/local/storage/path/" + fileName;
}else if(type.equals("remote")){
filePath = fileName
}
String key = prefix + filePath;
simpleMetadataStore.remove(key);
return key;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在获取我想要的文件。它正在为我重新获取文件。