0

please tell me how you can get files recursively from subdirectories?

I add an expression to select a remote directory to my IntegrationFlow - String fileNameRegex = "(\ d {4} | Success | Error |. *. Xml)"; Ftp.inboundStreamingAdapter (template ()). RemoteDirectory (new ValueExpression <> (fileNameRegex))

But this does not work, files are taken only from the root directory.

The directory structure is as follows:

/
1111/
    In/
    Out/
    Error/
    Success/

2222/
    In/
    Out/
    Error/
    Success/

I only need to get files from Error and Success *.xml

@Bean
public DelegatingSessionFactory<FTPFile> delegatingSessionFactory() {
    Map<Object, SessionFactory<FTPFile>> factories = new LinkedHashMap<>();
    for (int i = 0; i < this.names.length; i++) {
        DefaultFtpSessionFactory factory = new DefaultFtpSessionFactory();
        factory.setHost(this.hosts[i]);
        factory.setUsername(this.users[i]);
        factory.setPassword(this.pwds[i]);
        factories.put(this.names[i], factory);
    }
    return new DelegatingSessionFactory<>(factories, factories.values().iterator().next());
}

@Bean
public RotatingServerAdvice advice() {
    List<RotationPolicy.KeyDirectory> keyDirectories = new ArrayList<>();
    keyDirectories.add(new RotationPolicy.KeyDirectory(this.names[0], ""));
    keyDirectories.add(new RotationPolicy.KeyDirectory(this.names[1], ""));
    return new RotatingServerAdvice(delegatingSessionFactory(), keyDirectories);
}

@Bean
public FtpRemoteFileTemplate template() {
    return new FtpRemoteFileTemplate(delegatingSessionFactory());
}

@Bean
public IntegrationFlow ftpIntegrationFlow() {
    String fileNameRegex = "(\\d{4}|Success|Error|.*.xml)";
    return IntegrationFlows.from(
            Ftp.inboundStreamingAdapter(template()).remoteDirectory(new ValueExpression<>(fileNameRegex)),
            e -> e.poller(Pollers.fixedDelay(500).advice(advice())))
            .transform(new StreamTransformer("UTF-8"))
            .handle(message -> {
                log.info("Read file: {}", message.getHeaders()
                        .get("file_remoteDirectory" + "/" + message.getHeaders()
                                .get("file_remoteFile")));
                messageService.unmarshall(message);
            })
            .get();
}

mrtopgun
  • 25
  • 5

1 Answers1

1

It is not supported yet: https://github.com/spring-projects/spring-integration/issues/3407.

Those deep files are not visible from the root dir. You have to configure channel adapters for the specific sub-dir as a working root.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thank you for quick answer! – mrtopgun Mar 17 '21 at 22:43
  • You can use an outbound gateway with a recursive MGET command to copy the tree locally - https://docs.spring.io/spring-integration/docs/current/reference/html/ftp.html#ftp-outbound-gateway (or a recursive LS and another gateway to GET each file). https://docs.spring.io/spring-integration/docs/current/reference/html/ftp.html#using-the-mget-command – Gary Russell Mar 17 '21 at 22:49