2017-10-13 114 views
0

我的spring批处理项目需要从多个sftp服务器下载文件。 sftp主机/端口/文件路径配置在application.properties文件中。我考虑使用Spring集成的'ftp out-bound gateway'来连接这些服务器并下载文件。但我不知道如何做这种配置(我使用java配置,)并使其工作?我想我需要一些方法来定义多个会话工厂根据在application.properties文件中的sftp服务器信息配置的数量。春季集成:连接多台sftp服务器的解决方案/技巧?

属性文件:

sftp.host=host1,host2 
sftp.user=user1,user2 
sftp.pwd=pwd1,pwd2 

配置类:

@Bean 
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory1() { 

... 
} 

@Bean(name = "myGateway1") 
@ServiceActivator(inputChannel = "sftpChannel1") 
public MessageHandler handler1() { 

... 
} 

@MessagingGateway 
public interface DownloadGateway1 { 
@Gateway(requestChannel = "sftpChannel1") 
    List<File> start(String dir); 
} 

@Bean(name="sftpChannel1") 
public MessageChannel sftpChannel1() { 
    return new DirectChannel(); 
} 

回答

1

权,服务器会话工厂,而不是网关指定。该框架确实提供了委托会话工厂,允许从发送到网关的每条消息的配置工厂之一中选择它。见Delegating Session Factory

编辑

下面是一个例子:

@SpringBootApplication 
public class So46721822Application { 

    public static void main(String[] args) { 
     SpringApplication.run(So46721822Application.class, args); 
    } 

    @Value("${sftp.name}") 
    private String[] names; 

    @Value("${sftp.host}") 
    private String[] hosts; 

    @Value("${sftp.user}") 
    private String[] users; 

    @Value("${sftp.pwd}") 
    private String[] pwds; 

    @Autowired 
    private DelegatingSessionFactory<?> sessionFactory; 

    @Autowired 
    private SftpGateway gateway; 

    @Bean 
    public ApplicationRunner runner() { 
     return args -> { 
      try { 
       this.sessionFactory.setThreadKey("one"); // use factory "one" 
       this.gateway.send(new File("/tmp/f.txt")); 
      } 
      finally { 
       this.sessionFactory.clearThreadKey(); 
      } 
     }; 
    } 

    @Bean 
    public DelegatingSessionFactory<LsEntry> sessionFactory() { 
     Map<Object, SessionFactory<LsEntry>> factories = new LinkedHashMap<>(); 
     for (int i = 0; i < this.names.length; i++) { 
      DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(); 
      factory.setHost(this.hosts[i]); 
      factory.setUser(this.users[i]); 
      factory.setPassword(this.pwds[i]); 
      factories.put(this.names[i], factory); 
     } 
     // use the first SF as the default 
     return new DelegatingSessionFactory<LsEntry>(factories, factories.values().iterator().next()); 
    } 

    @ServiceActivator(inputChannel = "toSftp") 
    @Bean 
    public SftpMessageHandler handler() { 
     SftpMessageHandler handler = new SftpMessageHandler(sessionFactory()); 
     handler.setRemoteDirectoryExpression(new LiteralExpression("foo")); 
     return handler; 
    } 

    @MessagingGateway(defaultRequestChannel = "toSftp") 
    public interface SftpGateway { 

     void send(File file); 

    } 

} 

与性能...

sftp.name=one,two 
sftp.host=host1,host2 
sftp.user=user1,user2 
sftp.pwd=pwd1,pwd2 
+0

喜加里,我想设置与@注释一个类中的多个SFTP连接组态。连接信息是配置在属性文件中,我奋斗很长时间,但不能实现它,你可以提供一些提示?谢谢! [也见这篇文章](https://stackoverflow.com/questions/46802260/how-to-define-multiple-sftp-connections) –

+0

目前尚不清楚问题是什么;您可以在@ Configuration中轻松添加多个连接工厂,作为'@ Bean's,每个连接工厂都有自己的一组属性。如果那不是你的意思;请详细解释一下。 –

+0

嗨加里,我无法静态定义多个@Bean,因为连接的数量取决于属性文件。这些bean应该动态地注册到spring上下文中。 –