首页 > 解决方案 > 具有多个文件夹的 Sftp OutboundAdapter

问题描述

我需要将文件从本地传输到多个 sftp 服务器文件夹。到目前为止,这是我的代码,我正在使用单通道进行一对一的传输:

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

是否可以使用单通道将本地文件从一个本地文件夹传输到多个 sftp 文件夹?

标签: javaspring-integrationsftpspring-integration-sftp

解决方案


不,单个Sftp.outboundAdapter(). 它仅适用于单个远程目录,但是可以通过函数或表达式从请求消息中确定。但那是另一回事。

您的任务可以通过几个Sftp.outboundAdapter()远程目录和publishSubscribe配置来实现。像这样的东西:

.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")))
                )

推荐阅读