如何定义多个sftp连接?

时间:2017-10-18 03:22:29

标签: spring spring-integration

我正在尝试使用出站网关从多个sftp服务器下载文件,sftp服务器的数量在属性中配置(这意味着我需要动态定义连接),  我的application.properties:

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

当前我使用java配置为:

@Configuration

public class SFtpConfig {

static  Logger logger = LoggerFactory.getLogger(SFtpConfig.class);

@Autowired
SftpServerProperties sftpServerProperties;

@Autowired
SftpClientProperties sftpClientProperties;

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

 ...
}

@Bean(name = "myGateway")
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handlerLs() {

 ...
}

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

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

}

但是此配置仅用于连接到一个sftp服务器,那么如何使用application.properties配置定义多个sftp连接?  我的意思是sessionFactory / handler / gateway / channer的动态数量(一些bean有注释,如何动态定义这种bean?)

1 个答案:

答案 0 :(得分:0)

您应该考虑为ApplicationContext声明具有相同内容但不同属性的子ConnectionFactories

/**
 * Set the parent of this application context.
 * <p>Note that the parent shouldn't be changed: It should only be set outside
 * a constructor if it isn't available when an object of this class is created,
 * for example in case of WebApplicationContext setup.
 * @param parent the parent context
 * @see org.springframework.web.context.ConfigurableWebApplicationContext
 */
void setParent(@Nullable ApplicationContext parent);