首页 > 解决方案 > 列出远程服务器目录中的文件名

问题描述

我想递归地列出远程目录及其子目录中的文件。我知道可以通过调用 ListGateway 的 listFiles 方法来做到这一点:

列表列表 = listGateway.listFiles("/ussama/providers")

@MessagingGateway
public interface ListGateway {

    @Gateway(requestChannel = "listSftpChannel")
    List<File> listFiles(String dir);

}

@Bean
@ServiceActivator(inputChannel = "listSftpChannel")
public MessageHandler handler() {
    SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "ls", "'/directory'");
    return  sftpOutboundGateway;
}

@Bean
public IntegrationFlow sftpOutboundListFlow() {
    return IntegrationFlows.from("listSftpChannel")
            .handle(new SftpOutboundGateway(sftpSessionFactory(), "ls", "payload")
            ).get();
}

但我想每隔 x 分钟做一次。有没有办法可以轮询远程目录以在每 x 分钟后列出文件。请给java配置。

标签: javaspring-bootspring-integrationsftpspring-integration-sftp

解决方案


轮询目录的简单 POJO 消息源并根据需要配置轮询器...

@Bean
public IntegrationFlow pollLs(SessionFactory<LsEntry> sessionFactory) {
    return IntegrationFlows.from(() -> "foo/bar", e -> e
                .poller(Pollers.fixedDelay(5, TimeUnit.SECONDS)))
            .handle(Sftp.outboundGateway(sessionFactory, Command.LS, "payload")
                    .options(Option.RECURSIVE))
            .handle(System.out::println)
            .get();
}

显然,您将需要一些服务.handle来接收List<LsEntry>结果。

顺便说一句,有一个工厂类Sftp可以方便地创建端点。


推荐阅读