【问题标题】:Spring Integration DSL SFTP good practicesSpring Integration DSL SFTP 良好实践
【发布时间】:2016-06-16 21:37:59
【问题描述】:

我目前正在尝试使用 SI DSL SFTP 功能推送一些文件。

我不是很流利地使用这个 fwk,所以我想知道是否有更好的方法来完成我想要实现的目标。

有点像这样工作,除了文件被复制时,其余调用处于超时状态......

奖励:是否有一些关于 SI DSL 的好读物(书籍或在线)? (cafe si 样本和参考除外..)

编辑:

  • 此 SI 流程是否遵循 SI 良好实践?
  • 尽管文件已正确复制到 sftp 服务器,但为什么我的 rest 调用以超时结束?

Java 配置:

    @Configuration
    @EnableIntegration
    @IntegrationComponentScan
    public class IntegrationConfig {

        //flow gateway
        @MessagingGateway
        public interface FlowGateway {
            @Gateway(requestChannel = "SftpFlow.input")
            Collection<String> flow(Collection<File> name);

        }

        //sftp flow bean
        @Bean
        public IntegrationFlow SftpFlow() {
             return f -> f
                .split()
                .handle(Sftp.outboundAdapter(this.sftpSessionFactory(), FileExistsMode.REPLACE)
                        .useTemporaryFileName(false)
                        .remoteDirectory(sftpFolder));
        }

        //sftp session config
        @Bean
        public DefaultSftpSessionFactory sftpSessionFactory() {
            System.out.println("Create session");
            DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
            factory.setHost(sftpHost);
            factory.setPort(Integer.valueOf(sftpPort));
            factory.setUser(sftpUser);
            factory.setPassword(sftpPwd);
            factory.setAllowUnknownKeys(true);
            return factory;
        }

}

一个 RestController 类:

@Autowired
private FlowGateway flowGateway;

@RequestMapping("/testSftp")
public String testSftp() {
    flowGateway.flow(Arrays.asList(file1, file2, file3);
}

【问题讨论】:

    标签: java spring spring-integration sftp


    【解决方案1】:
    1. Spring Integration Java DSL 完全基于 Spring Integration Core。因此,所有概念、食谱、文档、示例等也都在这里应用。

    2. 问题是什么?让我猜猜:“为什么它会超时并阻塞?”这对我来说很明显,因为我知道在哪里阅读,但对其他人来说可能不清楚。请下次更具体一点:SO 上有足够多的人可以将您的问题关闭为“不清楚”。

    那么,让我们分析一下你拥有什么,以及为什么它不是你想要的。

    Spring Integration 中的端点可以是 one-way (Sftp.outboundAdapter()) 或 request-reply (Sftp.outboundGateway())。当它是one-way 时,没有任何东西可以返回并继续流程或发送回复,这并不奇怪,就像你的情况一样。

    我确定您对回复不感兴趣,否则您使用了不同的端点。

    .split() 发送所有项目后,该过程完全停止,并且没有任何内容可以发送回@Gateway,正如您的代码所暗示的那样:

    Collection<String> flow(Collection<File> name);
    

    具有非void 返回类型需要来自requestChannel 下游流的reply

    更多信息请参见Messaging Gateway

    也考虑使用

    .channel(c -> c.executor(taskExecutor()))
    

    .split() 之后将您的文件并行发送到 SFTP。

    附:我不确定您还需要阅读什么,因为到目前为止,您的代码中的一切都很好,只有这个讨厌的 reply 问题。

    【讨论】:

    • 非常明确的答案,正是我所期望的。我已根据您的评论编辑了我的问题。
    猜你喜欢
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2019-01-18
    • 2011-03-09
    • 2015-06-09
    • 2018-12-02
    相关资源
    最近更新 更多