java - Spring Integration DSL SFTP 良好实践

标签 java spring spring-integration sftp

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

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

它的工作方式是这样的,除了复制文件时,其余调用处于超时状态...

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

编辑:

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

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

最佳答案

  1. Spring Integration Java DSL 完全基于 Spring Integration 核心。因此,所有概念、方法、文档、示例等也适用于此处。

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

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

Spring Integration 中的端点可以是单向 (Sftp.outboundAdapter()) 或request-reply (Sftp.出站网关())。当它是单向时,没有什么可以返回并继续流程或发送回复,就像您的情况一样,这并不奇怪。

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

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

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

具有非 void 返回类型需要 requestChannel 上下游流的回复

参见 Messaging Gateway获取更多信息。

也考虑使用

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

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

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

关于java - Spring Integration DSL SFTP 良好实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37869749/

相关文章:

java - 测试私有(private)方法时不加载配置值

java - Spring MVC - 传递的 http 请求参数与相同的字符串值不匹配,为什么?

java - spring-integration http 出站网关不支持 é 等法语字符

java - 用于故障转移和恢复的最佳 Kafka 生产者选项

java - Spring 集成: TcpOutboundGateway getting DestinationResolutionException: no output-channel or replyChannel header available

java - 在外部 Java 项目中使用自定义内置函数

java - 如何从java上传jar文件到ooyala-spark作业服务器

java - 使用映射接口(interface)在类似 'struct' 的类实现中查找值

java - 使用 @InjectMocks 注入(inject) String 属性

spring - 如何扩展 Spring Boot 应用程序?