3

With Spring Integration, is it possible to synchronize files (including subfolder's files) recursively using ftp inbound channel adapter?

1 Answers1

1

No; but you can fetch a complete remote directory tree with recursive MGET with the outbound gateway...

@SpringBootApplication
@IntegrationComponentScan
public class So42324318Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(So42324318Application.class, args);
        List<File> files = context.getBean(Gateway.class).fetchFiles("foo/*");
        System.out.println(files);
        context.close();
    }

    @MessagingGateway(defaultRequestChannel = "fetchRecursive")
    public interface Gateway {

        public List<File> fetchFiles(String remoteDir);

    }

    @Bean
    @ServiceActivator(inputChannel = "fetchRecursive")
    public FtpOutboundGateway gateway() {
        // Create a recursive MGET gateway that gets the remote directory from the payload
        FtpOutboundGateway gateway = new FtpOutboundGateway(sessionFactory(), "mget", "payload");
        gateway.setOptions("-R"); 
        gateway.setLocalDirectoryExpression(new SpelExpressionParser().parseExpression("#remoteDirectory"));
        return gateway;
    }

    @Bean
    public SessionFactory<FTPFile> sessionFactory() {
        return new CachingSessionFactory<>(ftpSF());
    }

    private SessionFactory<FTPFile> ftpSF() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost("10.0.0.3");
        sf.setUsername("ftptest");
        sf.setPassword("ftptest");
        sf.setClientMode(FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE);
        return sf;
    }

}

Result:

2017-02-19 09:55:09.351  INFO 61921 --- [           main] o.s.integration.ftp.session.FtpSession   : File has been successfully transferred from: foo/bar.tx
2017-02-19 09:55:09.353  INFO 61921 --- [           main] o.s.integration.ftp.session.FtpSession   : File has been successfully transferred from: foo/bar.txt
2017-02-19 09:55:09.356  INFO 61921 --- [           main] o.s.integration.ftp.session.FtpSession   : File has been successfully transferred from: foo/bar/abc.txt
2017-02-19 09:55:09.358  INFO 61921 --- [           main] o.s.integration.ftp.session.FtpSession   : File has been successfully transferred from: foo/baz.txt
2017-02-19 09:55:09.362  INFO 61921 --- [           main] o.s.integration.ftp.session.FtpSession   : File has been successfully transferred from: foo/foo/bar/qux.txt
2017-02-19 09:55:09.364  INFO 61921 --- [           main] o.s.integration.ftp.session.FtpSession   : File has been successfully transferred from: foo/foo/baz/fiz.txt
[foo/bar.tx, foo/bar.txt, foo/bar/abc.txt, foo/baz.txt, foo/foo/bar/qux.txt, foo/foo/baz/fiz.txt]
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Thanks @Gary for the quick response. With your code i could get the files with the directory hierarchy.I need to poll and get the files when it is updated on the remote server, how do i do it with the outbound gateway.Basically i need to synchronize the local files (with the tree structure intact) with the remote. – user2654631 Feb 22 '17 at 04:14
  • If you set the `FileExistsMode` to IGNORE, each MGET will only get new files that have not previously been fetched. The 5.0 release will have [a new mode](https://github.com/spring-projects/spring-integration/pull/2062) `REPLACE_IF_MODIFIED` which will refetch files that exist but have different timestamps (I created the PR yesterday). Another possible solution for you is to use a [smart poller](http://docs.spring.io/spring-integration/reference/html/messaging-channels-section.html#__smart_polling) with the inbound adapter and change the remote and local directories when the poll returns null. – Gary Russell Feb 22 '17 at 13:56