Spring Integration ftp inboud channel processing files in specific order

社会主义新天地 提交于 2019-12-24 10:29:04

问题


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:


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;
    }

}


来源:https://stackoverflow.com/questions/49462148/spring-integration-ftp-inboud-channel-processing-files-in-specific-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!