May12
May12

Reputation: 2520

How to synchronise files from more than one folder between Ftp and local folder using Spring Integration?

Collegues, i'am trying to download folders with files from sftp using spring integration.

I can synchronised files from one single folders 20161207:

@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
   SftpInboundFileSynchronizer fileSynchronizer =
      new SftpInboundFileSynchronizer(sftpSessionFactory());
   fileSynchronizer.setDeleteRemoteFiles(false);
   fileSynchronizer.setRemoteDirectory("/pub/op/20161207/");
   fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.xml"));
   return fileSynchronizer;
}

But what should i do if need to download (synchronised) files from more than one folder (20161208, 20161209 etc)? Have you any example? Thank you.

UPDATE I tried to use SftpRegexPatternFileListFilter, but it didn't help:

  @Bean
    public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
        SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
        fileSynchronizer.setDeleteRemoteFiles(false);
        fileSynchronizer.setRemoteDirectory("/pub/op/");
        Pattern pattern = Pattern.compile(".*\\.xml$");
        SftpRegexPatternFileListFilter sftpRegexPatternFileListFilter  = new SftpRegexPatternFileListFilter(pattern);
        fileSynchronizer.setFilter(sftpRegexPatternFileListFilter);
        //fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.xml"));
        return fileSynchronizer;
    }

UPDATE Code is replac4ed here according to advice:

@Bean @ServiceActivator(inputChannel = "sftpChannel") 
    public MessageHandler handler() 
    { SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "MGET", "payload"); 
        sftpOutboundGateway.setLocalDirectory(new File("sftp-inbound")); 
        return sftpOutboundGateway;}

UPDATE Artem, Gary, thank you for help. This is my code: @Bean public DefaultSftpSessionFactory sftpSessionFactory() {

        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
        factory.setHost(server);
        factory.setPort(port);
        factory.setAllowUnknownKeys(true);
        factory.setUser(login);
        factory.setPassword(pass);
        factory.setTimeout(60*1000);
        return factory;
    }


@Bean
    @ServiceActivator(inputChannel = "sftpChannel")
    public MessageHandler handler() {
        SftpOutboundGateway sftpOutboundGateway = new  SftpOutboundGateway(sftpSessionFactory(), "mget", "getPayload() == '/pub/op/20170130/test.xml'");
        sftpOutboundGateway.setLocalDirectory(new File("C:/test/gateway/"));
        return sftpOutboundGateway;
    }

When i start application i receive the next output

12:25:58.593 INFO  [main] o.s.c.s.DefaultLifecycleProcessor : Starting beans in phase -2147483648
12:25:58.593 INFO  [main] o.s.i.endpoint.EventDrivenConsumer : Adding {sftp:outbound-gateway:aameFtpConfig.handler.serviceActivator} as a subscriber to the 'sftpChannel' channel
12:25:58.593 INFO  [main] o.s.i.channel.DirectChannel : Channel 'org.springframework.context.annotation.AnnotationConfigApplicationContext@4157f54e.sftpChannel' has 1 subscriber(s).
12:25:58.593 INFO  [main] o.s.i.endpoint.EventDrivenConsumer : started aameFtpConfig.handler.serviceActivator
12:25:58.594 INFO  [main] o.s.c.s.DefaultLifecycleProcessor : Starting beans in phase 0
12:25:58.594 INFO  [main] o.s.i.endpoint.EventDrivenConsumer : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
12:25:58.594 INFO  [main] o.s.i.c.PublishSubscribeChannel : Channel 'org.springframework.context.annotation.AnnotationConfigApplicationContext@4157f54e.errorChannel' has 1 subscriber(s).
12:25:58.594 INFO  [main] o.s.i.endpoint.EventDrivenConsumer : started _org.springframework.integration.errorLogger

But file is not copied into "C:/test/gateway/". Plase, tel me what i did wrong?

Upvotes: 1

Views: 1591

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121550

Try to use SftpOutboundGateway with MGET command.

That dynamic remote directory can be as request message.

The polling logic you can achieve with raw method invoking Inbound Channel Adapter. And this one can generate the directory name to send.

You may consider to use Idempotent Receiver after gateway to be sure do not process the same files again.

The Sftp Inbound Channel Adapter can't do that for you because its purpose to check the same resource for new data.

Upvotes: 1

Related Questions