使用Spring Integration从远程SFTP目录和子目录流式传输

问题描述 投票:0回答:1

我正在使用Spring Integration Streaming入站通道适配器,以从远程SFTP获取流并解析内容处理的每一行。

我使用:

IntegrationFlows.from(Sftp.inboundStreamingAdapter(template)
                          .filter(remoteFileFilter)
                          .remoteDirectory("test_dir"),
                        e -> e.id("sftpInboundAdapter")
                              .autoStartup(true)
                              .poller(Pollers.fixedDelay(fetchInt)))
                .handle(Files.splitter(true, true))
....

并且它现在可以工作。但是我只能从test_dir目录中获取文件,但是我需要从该目录和子目录中递归获取文件并解析每一行。

我注意到Inbound Channel AdapterSftp.inboundAdapter(sftpSessionFactory).scanner(...)。它可以扫描子目录。但是我没有看到Streaming Inbound Channel Adapter的任何内容。

所以,如何在Streaming Inbound Channel Adapter中实现“从目录递归获取文件?”

谢谢。

spring-integration spring-integration-dsl spring-integration-sftp
1个回答
0
投票

您可以使用两个出站网关-首先使用ls -R(递归列表);分割结果,并使用配置为mget -stream的网关获取每个文件。

编辑

@SpringBootApplication
public class So60987851Application {

    public static void main(String[] args) {
        SpringApplication.run(So60987851Application.class, args);
    }

    @Bean
    IntegrationFlow flow(SessionFactory<LsEntry> csf) {
        return IntegrationFlows.from(() -> "foo", e -> e.poller(Pollers.fixedDelay(5_000)))
                .handle(Sftp.outboundGateway(csf, Command.LS, "payload")
                        .options(Option.RECURSIVE, Option.NAME_ONLY)
                        // need a more robust metadata store for persistence, unless the files are removed
                        .filter(new SftpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "test")))
                .split()
                .log()
                .enrichHeaders(headers -> headers.headerExpression("fileToRemove", "'foo/' + payload"))
                .handle(Sftp.outboundGateway(csf, Command.GET, "'foo/' + payload")
                        .options(Option.STREAM))
                .split(new FileSplitter())
                .log()
                // instead of a filter, we can remove the remote file.
                // but needs some logic to wait until all lines read
//              .handle(Sftp.outboundGateway(csf, Command.RM, "headers['fileToRemove']"))
//              .log()
                .get();
    }

    @Bean
    CachingSessionFactory<LsEntry> csf(DefaultSftpSessionFactory sf) {
        return new CachingSessionFactory<>(sf);
    }

    @Bean
    DefaultSftpSessionFactory sf() {
        DefaultSftpSessionFactory sf = new DefaultSftpSessionFactory();
        sf.setHost("10.0.0.8");
        sf.setUser("gpr");
        sf.setPrivateKey(new FileSystemResource(new File("/Users/grussell/.ssh/id_rsa")));
        sf.setAllowUnknownKeys(true);
        return sf;
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.