列出远程服务器目录中的文件名

时间:2019-01-23 09:45:51

标签: java spring-boot spring-integration sftp spring-integration-sftp

我想递归地列出远程目录及其子目录中的文件。我知道可以通过调用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配置。

1 个答案:

答案 0 :(得分:1)

轮询目录的简单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具有用于创建端点的便捷方法。