1
votes

I'm using Spring Integration to poll files from remote FTP server and process them.

Is there a way to configure FtpInboundFileSynchronizer (or other component) to fetch and process remote files in specific order. Say i have file1 and file2 in remote directory, is it possible to fetch and process file1 before file2.

Thanks in advance

1

1 Answers

1
votes

There are (at least) 3 techniques to achieve this:

  1. Add a custom FileListFilter<FTPFile> (that sorts the FTPFile objects into the order you desire) to the synchronizer.

  2. Use two FTP outbound gateways, one the list (ls) the files, and one to get each file as needed.

  3. Use the FtpRemoteFileTemplate from within your own code to list and fetch files.

EDIT

Actually, for #1, you would also need a custom FileListFilter<File> in the local filter to sort the File objects. Since the local files are emitted as message payloads after the synchronization is complete.

EDIT2 Remote file template example

This just copies the first file in the list, but it should give you what you need...

@SpringBootApplication
public class So49462148Application {

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

    @Bean
    public ApplicationRunner runner(FtpRemoteFileTemplate template) {
        return args -> {
            FTPFile[] files = template.list("*.txt");
            System.out.println(Arrays.toString(files));
            template.get(files[0].getName(), is -> {
                File file = new File("/tmp/" + files[0].getName());
                FileOutputStream os = new FileOutputStream(file);
                FileCopyUtils.copy(is, os);
                System.out.println("Copied: " + file.getAbsolutePath());
            });
        };
    }

    @Bean
    public DefaultFtpSessionFactory sf() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost("...");
        sf.setUsername("...");
        sf.setPassword("...");
        return sf;
    }

    @Bean
    public FtpRemoteFileTemplate template(DefaultFtpSessionFactory sf) {
        FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sf);
        template.setRemoteDirectoryExpression(new LiteralExpression("foo"));
        return template;
    }

}