如何动态连接其他SFTP服务器?

时间:2020-06-08 06:46:37

标签: java spring-boot spring-integration spring-integration-sftp

我正在使用spring集成将文件从SFTP移到本地目录。我能够将文件从一个sftp服务器移动到本地,我有3台4 sftp服务器,首先我考虑为每个SFTP服务器编写不同的类,经过研究,我发现我们可以使用委派sessionfactory 使用Java设置多个SFTP。我阅读了文档,但无法实现。任何人都可以帮助我。我在下面添加我的代码。

@Configuration
@EnableIntegration
public class SftpFileMove {

    @Value("${sftpConfig.host}")
    private String host;

    @Value("${sftpConfig.username}")
    private String userName;

    @Value("${sftpConfig.password}")
    private String password;

    @Value("${sftpConfig.port}")
    private Integer port;

    @Bean
    public SessionFactory<LsEntry> sftpSessionFactory() {
        final DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost(host);
        factory.setPort(port);
        factory.setUser(userName);
        factory.setPassword(password);
        factory.setAllowUnknownKeys(true);
        return new CachingSessionFactory<LsEntry>(factory);
    }

    @Bean
    public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
        SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
        fileSynchronizer.setDeleteRemoteFiles(true);
        fileSynchronizer.setRemoteDirectory("/upload/INV/");
        fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.xml"));
        return fileSynchronizer;
    }

    @Bean
    @InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "30000"))
    public MessageSource<File> sftpMessageSource() {
        SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
                sftpInboundFileSynchronizer());
        source.setLocalDirectory(new File("feeds/"));
        source.setAutoCreateLocalDirectory(true);
        source.setLocalFilter(new AcceptOnceFileListFilter<File>());
        return source;
    }

    @Bean
    @ServiceActivator(inputChannel = "sftpChannel")
    public MessageHandler handler() {
        return new MessageHandler() {

            @Override
            public void handleMessage(Message<?> message) throws MessagingException {
                BatchProcessorLogger.debug("F111F7B0-9235-11EA-AB12-0800200C9A66", "Moved succussfully to{}",
                        message.getPayload());
            }

        };
    }
}

1 个答案:

答案 0 :(得分:0)

请参见Inbound Channel Adapters: Polling Multiple Servers and Directories

只需为每个服务器配置一个会话工厂,然后将它们添加到委派工厂中,每个服务器都有一个密钥。

然后设置RotatingServerAdvice以循环浏览服务器/目录。