How to get files recursively from subdirectories spring-integration

681 Views Asked by At

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

1

There are 1 best solutions below

2
On

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.