如何配置Spring集成多个网关?

问题描述 投票:0回答:1

处理多SFTP主机在Spring集成

我想这样配置基于用户输入(用户通过主机名)多SFTP主机,我将选择主机并进行读操作。

问题是我不想创建多个Java文件。相反,我想输入主机细节在属性文件中的阵列,并基于该输入,配置创建1,2或更多SessionFactories,网关等。

下面是我用来设置单个主机的配置.....

@Configuration
public class HostConfiguration {

  @Bean
  public SessionFactory<LsEntry> sftpSessionFactory() {

    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setPort(22);
    factory.setHost(host);
    factory.setUser(user);
    factory.setPassword(password);
    factory.setAllowUnknownKeys(true);

    return new CachingSessionFactory<LsEntry>(factory);
  }

  @Bean
  @ServiceActivator(inputChannel = "toSftpReadChannel")
  @Description("Sftp Outbound Gateway for SFTP Read Operation")
  public MessageHandler sftpReadHandler() {

    SftpOutboundGateway sftpReadGateway = new SftpOutboundGatewaysftpSessionFactory(), Command.LS.getCommand(), "payload");
    sftpReadGateway.setAsync(true);
    sftpReadGateway.setOption(Option.NAME_ONLY);
    sftpReadGateway.setOutputChannel(fromSftpReadChannel());

    return sftpReadGateway;
  }

  @Bean(name = PollerMetadata.DEFAULT_POLLER)
  public PollerMetadata poller() {
   return Pollers.fixedRate(500).get();
  }

  @Bean
  @Description("Sftp Read Request Channel")
  public MessageChannel toSftpReadChannel(){
    return new QueueChannel(5);
  }

  @Bean
  @Description("Sftp Read Response Channel")
  public MessageChannel fromSftpReadChannel(){
    return new DirectChannel();
  }

  @MessagingGateway
  public interface SftpGateway {

    @Gateway(requestChannel = "toSftpReadChannel", replyChannel = "fromSftpReadChannel")
    Future<Message> readFromRemoteSftp(Message message);

  }

}

任何想法如何在Spring的启动/ Spring的集成实现这一!!!!

java spring spring-integration
1个回答
1
投票

你可能会考虑使用DelegatingSessionFactory代替多种配置:

/**
 * {@link SessionFactory} that delegates to a {@link SessionFactory} retrieved from a
 * {@link SessionFactoryLocator}.
 *
 * @author Gary Russell
 * @since 4.2
 *
 */
public class DelegatingSessionFactory<F> implements SessionFactory<F> {

更多信息请参见文档:https://docs.spring.io/spring-integration/docs/current/reference/html/ftp.html#ftp-dsf

© www.soinside.com 2019 - 2024. All rights reserved.