如何动态连接不同的SFTP服务器?

问题描述:

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

i am using spring integration to move a file from SFTP to local directory. i am able to move files from one sftp server to local, i have 3 4 sftp servers, first i thought about writing different classes for each SFTP server, after my research i found that we can use delegating sessionfactory for setting more than one SFTP using java. i read the documentation but i couldn't implement that. anybody can help me with that. i am adding my code below.

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

        };
    }
}

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

Simply configure a session factory for each server an add them to the delegating factory, each with a key.

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

Then set up the RotatingServerAdvice to cycle through the servers/directories.