Search code examples
javaspring-bootspring-integrationsftpspring-integration-sftp

list file names from a remote server directory


I want to list the files recursively in a remote directory and its subdirectories as well. I know can do this by calling the listFiles method of ListGateway as:

List list = 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();
}

But I want to do it after every x minutes. Is there a way i can poll the remote directory to list files after every x minutes. Plz give java configuration.


Solution

  • Poll a simple POJO message source for the directory and configure the poller as needed...

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

    Obviously you will need some service in the .handle to receive the List<LsEntry> result.

    By the way, there is a factory class Sftp with convenient methods for creating endpoints.