Linked Questions

Popular Questions

How to configure multiple gateways in Spring Integration?

Asked by At

Handling Multiple-SFTP Hosts in Spring Integration

I want to configure multiple SFTP hosts so based on user input (user passing hostName), I will select the HOST and perform read operation.

The issue is I don't wanna create multiple Java files. instead, I wanna input the host details as an array in properties files and based on that input, the configuration create 1, 2 or more SessionFactories, Gateways and etc.

Following is the configuration that I used to setup single Host.....

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

  }

}

Any idea how to achieve this in Spring-Boot/Spring-Integration!!!!

Related Questions