【问题标题】:Remote directory for sftp outbound gateway with DSL带有 DSL 的 sftp 出站网关的远程目录
【发布时间】:2018-01-19 12:43:48
【问题描述】:

我在使用 DSL 的 SFTP 出站网关方面遇到问题。 我想使用出站网关发送文件,然后继续我的流程。 问题是我有一个异常告诉我:

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

我不知道如何告诉我的出站网关哪个是目录,具体取决于我的标题中的内容。

【问题讨论】:

    标签: spring-integration sftp dsl


    【解决方案1】:

    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"))
    

    【讨论】:

    • 感谢您的回答,但我很难知道如何在流程中设置目录表达式。如果我理解的话,我会将 RemoteFileTemplate 声明为 Bean,在我的网关中调用它之前,我需要添加一个更改目录表达式的步骤?
    • ???为什么需要更改目录表达式?你说你有一个关于这个问题的标题,所以你只需要一个表达式来访问每条消息的标题。这正是我的答案。然后,您需要将 RemoteFileTemplate 用于 Sftp.outboundGateway() 定义。
    • 抱歉,我不习惯 SpelExpression,我以为我必须从我的流程中发送 Expression,但没有。我现在知道了。谢谢
    【解决方案2】:

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

    // 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 的问题。这就是你有这个例外的原因。
    猜你喜欢
    • 2023-03-08
    • 2017-06-08
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    相关资源
    最近更新 更多