Sftp OutboundAdapter with multiple folders

Viewed 219

I need to transfer the file from Local to multiple sftp server folders. This is my code so far, I'm transferring one to one basis with single channel:

private IntegrationFlow localToRemotePush(final String localDirectory,String remoteDirectory, String adapterName) {    
    return IntegrationFlows
            .from(Files.inboundAdapter(Paths.get(localDirectory).toFile())
                                .regexFilter(FILE_PATTERN_REGEX)
                                .preventDuplicates(false),
                        e -> {
                            e.poller(Pollers.fixedDelay(getPollerIntervalMs())
                                    .maxMessagesPerPoll(getMaxFetchSize())
                                    .errorChannel("errorChannel")
                                    .transactional(transactionManager)
                                    .transactionSynchronizationFactory(mmPushSftpSyncFactory()) // moves processed files
                            ).id(adapterName);
                        })
            .handle(Sftp.outboundAdapter(mmPushSftpSessionFactory())
                        .remoteDirectory(getRemoteRootDir() + remoteDirectory1)
                        //.remoteDirectory(getRemoteRootDir() + remoteDirectory2) --- this way is correct ?
                        .temporaryFileSuffix(".tmp"))
            .get();
}

Is it possible to transfer local files from one local folder to multiple sftp folders using single channel?

1 Answers

No, it's not possible with a single Sftp.outboundAdapter(). It is designed only for a single remote directory which, however, can be determined from the request message by the function or expression. But that's different story.

Your task can be achieved with several Sftp.outboundAdapter() for each remote directory and publishSubscribe configuration. Something like this:

.publishSubscribeChannel(s -> s
                        .subscribe(f -> f
                                .handle(Sftp.outboundAdapter(mmPushSftpSessionFactory())
                                          .remoteDirectory(getRemoteRootDir() + remoteDirectory1)
                                          .temporaryFileSuffix(".tmp")))
                        .subscribe(f -> f
                                .handle(Sftp.outboundAdapter(mmPushSftpSessionFactory())
                                          .remoteDirectory(getRemoteRootDir() + remoteDirectory2)
                                          .temporaryFileSuffix(".tmp")))
                )
Related