首页 文章

带有DSL的sftp出站网关的远程目录

提问于
浏览
2

我在使用DSL的SFTP出站网关时遇到问题 . 我想使用出站网关发送文件,然后继续我的流程 . 问题是我有一个例外告诉我:

IllegalArgumentException: 'remoteDirectoryExpression' is required

我看到我可以使用RemoteFileTemplate,我可以在其中设置sftp会话工厂和远程目录信息,但是我不想通过在批处理启动之前放入 Headers 中的代码在我的流程中定义目录 .

@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();
}

我不知道如何告诉我的出站网关哪个目录取决于我的 Headers 中的内容 .

2 回答

  • 2

    Sftp.outboundGateway() 具有 RemoteFileTemplate 的重载版本 . 所以,你需要实例化 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"))
    
  • 1

    在我得到答案之前,我提出了这个解决方案,但我不确定它是一个好的解决方案 .

    // 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 抛出异常

相关问题