【问题标题】:Spring Integration ftp inbound channel adapter recursive synchronizationSpring Integration ftp 入站通道适配器递归同步
【发布时间】:2017-02-19 06:24:41
【问题描述】:

使用 Spring Integration,是否可以使用 ftp 入站通道适配器递归同步文件(包括子文件夹的文件)?

【问题讨论】:

    标签: recursion ftp spring-integration adapter inbound


    【解决方案1】:

    没有;但是您可以通过出站网关使用递归 MGET 获取完整的远程目录树...

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

    结果:

    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 的快速回复。使用您的代码,我可以获取具有目录层次结构的文件。我需要在远程服务器上更新文件时轮询并获取文件,我如何使用出站网关进行操作。基本上我需要同步本地文件(使用树结构完好)与遥控器。
    • 如果您将FileExistsMode 设置为 IGNORE,则每个 MGET 将只获取以前未获取的新文件。 5.0 版本将具有a new modeREPLACE_IF_MODIFIED,它将重新获取存在但具有不同时间戳的文件(我昨天创建了 PR)。另一个可能的解决方案是将smart poller 与入站适配器一起使用,并在轮询返回 null 时更改远程和本地目录。
    猜你喜欢
    • 2014-06-29
    • 2023-03-12
    • 1970-01-01
    • 2016-07-19
    • 2016-11-17
    • 2017-10-08
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    相关资源
    最近更新 更多