Spring integration 带有DSL的sftp出站网关的远程目录
我对使用DSL的SFTP出站网关有问题。 我想使用出站网关发送文件,然后继续我的流程。 问题是我有一个例外告诉我:Spring integration 带有DSL的sftp出站网关的远程目录,spring-integration,sftp,dsl,Spring Integration,Sftp,Dsl,我对使用DSL的SFTP出站网关有问题。 我想使用出站网关发送文件,然后继续我的流程。 问题是我有一个例外告诉我: IllegalArgumentException: 'remoteDirectoryExpression' is required 我看到我可以使用RemoteFileTemplate,在这里我可以设置sftp会话工厂和远程目录信息,但是我不想要的目录是在我的流中通过在批处理启动之前放入头中的代码定义的 @Bean public IntegrationFlow order
IllegalArgumentException: 'remoteDirectoryExpression' is required
我看到我可以使用RemoteFileTemplate,在这里我可以设置sftp会话工厂和远程目录信息,但是我不想要的目录是在我的流中通过在批处理启动之前放入头中的代码定义的
@Bean
public IntegrationFlow orderServiceFlow() {
return f -> f
.handleWithAdapter(h -> h.httpGateway("myUrl")
.httpMethod(HttpMethod.GET)
.expectedResponseType(List.class)
)
.split()
.channel(batchLaunchChannel());
}
@Bean
public DirectChannel batchLaunchChannel() {
return MessageChannels.direct("batchLaunchChannel").get();
}
@Bean
public IntegrationFlow batchLaunchFlow() {
return IntegrationFlows.from(batchLaunchChannel())
.enrichHeaders(h -> h
.headerExpression("oiCode", "payload")
)
.transform((GenericTransformer<String, JobLaunchRequest>) message -> {
JobParameters jobParameters = new JobParametersBuilder()
.addDate("exec_date", new Date())
.addString("oiCode", message)
.toJobParameters();
return new JobLaunchRequest(orderServiceJob, jobParameters);
})
.handle(new JobLaunchingMessageHandler(jobLauncher))
.enrichHeaders(h -> h
.headerExpression("jobExecution", "payload")
)
.handle((p, h) -> {
//Logic to retreive file...
return new File("");
})
.handle(Sftp.outboundGateway(sftpSessionFactory,
AbstractRemoteFileOutboundGateway.Command.PUT,
"payload")
)
.get();
}
@Bean
公共集成流orderServiceFlow(){
返回f->f
.handleWithAdapter(h->h.httpGateway(“myUrl”)
.httpMethod(httpMethod.GET)
.expectedResponseType(List.class)
)
.split()
.channel(batchLaunchChannel());
}
@豆子
公共DirectChannel batchLaunchChannel(){
返回MessageChannels.direct(“batchLaunchChannel”).get();
}
@豆子
公共集成流batchLaunchFlow(){
返回IntegrationFlows.from(batchLaunchChannel())
.enrichHeaders(h->h
.headerExpression(“oiCode”、“payload”)
)
.transform((GenericTransformer)消息->{
JobParameters JobParameters=新的JobParametersBuilder()
.addDate(“执行日期”,新日期())
.addString(“oiCode”,消息)
.toJobParameters();
返回新的JobLaunchRequest(orderServiceJob,jobParameters);
})
.handle(新JobLaunchingMessageHandler(jobLauncher))
.enrichHeaders(h->h
.headerExpression(“作业执行”、“有效负载”)
)
.手柄((p,h)->{
//检索文件的逻辑。。。
返回新文件(“”);
})
.手柄(Sftp.外部网关(sftpSessionFactory、,
AbstractRemoteFileOutboundGateway.Command.PUT,
“有效载荷”)
)
.get();
}
我不知道如何根据标题中的内容来告诉出站网关哪个目录将成为目录。。因此,您需要实例化
SftpRemoteFileTemplate
bean并配置其:
/**
* Set the remote directory expression used to determine the remote directory to which
* files will be sent.
* @param remoteDirectoryExpression the remote directory expression.
*/
public void setRemoteDirectoryExpression(Expression remoteDirectoryExpression) {
这个函数可以类似于FunctionExpression
:
setRemoteDirectoryExpression(m -> m.getHeaders().get("remoteDireHeader"))
在我得到答案之前,我提出了这个解决方案,但我不确定它是否是一个好的解决方案
// flow //
.handle((p, h) -> {
//Logic to retreive file...
return new File("");
})
.handle(
Sftp.outboundGateway(
remoteFileTemplate(new SpelExpressionParser().parseExpression("headers['oiCode']")),
AbstractRemoteFileOutboundGateway.Command.PUT,
"payload")
)
.handle(// next steps //)
.get();
public RemoteFileTemplate remoteFileTemplate(Expression directory) throws Exception {
RemoteFileTemplate template = new SftpRemoteFileTemplate(sftpSessionFactory);
template.setRemoteDirectoryExpression(directory);
template.setAutoCreateDirectory(true);
template.afterPropertiesSet();
return template;
}
但这会引发警告,因为ExpresionUtils
java.lang.RuntimeException:No beanFactory
谢谢您的回答,但是我很难知道如何在流中设置目录表达式。如果我理解,我将把RemoteFileTemplate声明为Bean,在我的网关中调用它之前,我需要添加一个步骤来更改目录表达式????为什么需要更改目录表达式?你说你有一个关于这个问题的标题,所以你只需要一个表达式就可以访问每条消息的标题。这正是我的答案。然后你需要使用RemoteFileTemplate
来定义Sftp.outboundGateway()
。对不起,我不习惯SpelExpression,我以为我必须从流中发送表达式,但是没有。我现在明白了。谢谢,我的回答和你完全一样。唯一的问题是您没有将RemoteFileTemplate
声明为bean。这就是为什么你有这个例外。