Spring integration spring集成:连接多个sftp服务器的解决方案/技巧?

Spring integration spring集成:连接多个sftp服务器的解决方案/技巧?,spring-integration,spring-batch,Spring Integration,Spring Batch,我的spring批处理项目需要从多个sftp服务器下载文件。 sftp主机/端口/文件路径在application.properties文件中配置。我考虑使用Spring Connect的SFTP输出绑定网关来连接这些服务器和下载文件。但我不知道如何进行这种配置(我使用的是java配置)并使其工作?我想我需要一些方法来根据application.properties文件中sftp服务器信息配置的数量来定义多个会话工厂 属性文件: sftp.host=host1,host2 sftp.user=u

我的spring批处理项目需要从多个sftp服务器下载文件。 sftp主机/端口/文件路径在application.properties文件中配置。我考虑使用Spring Connect的SFTP输出绑定网关来连接这些服务器和下载文件。但我不知道如何进行这种配置(我使用的是java配置)并使其工作?我想我需要一些方法来根据application.properties文件中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();
}
@Bean
公共会话工厂sftpSessionFactory1(){
...
}
@Bean(name=“myGateway1”)
@ServiceActivator(inputChannel=“sftpChannel 1”)
公共消息处理程序handler1(){
...
}
@消息网关
公共接口下载网关1{
@网关(requestChannel=“sftpChannel1”)
列表开始(字符串目录);
}
@Bean(name=“sftpChannel1”)
公共消息频道sftpChannel1(){
返回新的DirectChannel();
}

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

编辑

下面是一个例子:

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

    }

}

您好,Gary,我想用一个带有@Configuration注释的类设置多个sftp连接。连接信息是在属性文件中配置的,我已经挣扎了很长时间,但无法实现它,你能给我一些提示吗?谢谢问题是什么还不清楚;您可以轻松地在
@配置
中添加多个连接工厂作为
@Bean
,每个连接工厂都有自己的属性集。如果你不是这个意思;请更详细地解释。嗨,Gary,我不能静态地定义多个@Bean,因为连接的数量取决于属性文件。这些bean应该动态注册到spring上下文中。用一个示例属性文件编辑问题,我会告诉你怎么做。嗨,Gary,真的很抱歉回复晚了(我最近生病住院),我已经编辑了这个问题,你能帮我一点忙吗,谢谢
sftp.name=one,two
sftp.host=host1,host2
sftp.user=user1,user2
sftp.pwd=pwd1,pwd2