我正在尝试构建一个IntegrationFlow,在其中我从远程SFTP目录中读取文件列表,然后一步一步下载。我有多个目录要轮询,每个目录的标题中都有它自己的会话工厂。单个文件名来自第一个IntegrationFlow的有效负载,该负载为我检索了文件列表。
这里的问题是我无法将标头和有效负载发送给创建OutboundGateway的方法。
这是我要实现的代码:
private IntegrationFlow sftpDownloadFlow(RegionalSFTConfig regionalSFTConfig, SessionFactory regionalSftpSessionFactory, String region) {
return IntegrationFlows.
from(ftpSource(regionalSFTConfig),
configure -> configure.poller(Pollers.fixedRate(regionalSFTConfig.getPollInterval())))
.enrichHeaders((headers) -> {
headers.header("sftSessionFactory",regionalSftpSessionFactory);
headers.header("sftConfig",regionalSFTConfig);
})
.handle(getRemoteFileList(regionalSFTConfig, regionalSftpSessionFactory))
.filter(Objects::nonNull)
.split(FileInfo.class, FileInfo::getFilename)
.channel("sftp.download")
.get();
}
@Bean
public IntegrationFlow sftpRegion1Flow() {
return sftpDownloadFlow(region1Config, region1SessionFactory);
}
@Bean
public IntegrationFlow sftpRegion2Flow() {
return sftpDownloadFlow(region2Config, region2SessionFactory);
}
private AbstractRemoteFileOutboundGateway<ChannelSftp.LsEntry> getRemoteFileOutboundGateway(MessageHeaders headers, String fileName) {
AbstractRemoteFileOutboundGateway<ChannelSftp.LsEntry> fileOutboundGateway =
Sftp.outboundGateway((SessionFactory)headers.get("sftSessionFactory"),
AbstractRemoteFileOutboundGateway.Command.GET,
new SpelExpressionParser().parseExpression( "'" + ((String)headers.get("file_remoteDirectory")).concat(fileName) + "'").getExpressionString())
//.localDirectory(new File(regionalSFTConfig.getLocalDirectory()))
.options(AbstractRemoteFileOutboundGateway.Option.STREAM, AbstractRemoteFileOutboundGateway.Option.PRESERVE_TIMESTAMP)
.get();
fileOutboundGateway.setFileExistsMode(FileExistsMode.REPLACE);
return fileOutboundGateway;
}
@Bean
public IntegrationFlow DownloadAndProcessFlow() {
return IntegrationFlows.
from("sftp.download")
.handle(getRemoteFileOutboundGateway(headers, payload))
.handle(someFileProcessor)
.get();
}
在DownloadAndProcessFlow中-我希望能够在运行时将标头和有效负载发送到getRemoteFileOutboundGateway方法,以基于标头中的SFTConfig和SessionFactory创建网关。但是,在此阶段,我无法处理标头和有效负载。有指针吗?