Search code examples
spring-bootsftpspring-integration-sftp

Using different sftp session factories


Currently i upload all my generated files to single SFTP and all works perfectly. But requirements are changed and i need to upload file on different SFTP server. Example: On REST endpoint i got request type=FULL, then i need to upload file to SFTP1, if type=PART, then i need to upload to SFTP2.

My sftp config:

  @Bean
  public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
    final DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(properties.getHost());
    factory.setPort(properties.getPort());
    factory.setUser(properties.getUser());
    factory.setPassword(properties.getPassword());
    factory.setAllowUnknownKeys(true);
    return factory;
  }

  @Bean
  @ServiceActivator(inputChannel = "toSftpChannel")
  public MessageHandler handler(@Value("'${sftp.folder}'.concat(headers['type'])")
                                  final String remoteDirectory) {
    final SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
    handler.setRemoteDirectoryExpressionString(remoteDirectory);
    handler.setAutoCreateDirectory(true);
    return handler;
  }

@MessagingGateway
public interface SftpService {

  @Gateway(requestChannel = "toSftpChannel")
  void uploadFileToSftp(@Payload File file,
                        @Header("filename") String filename,
                        @Header("type") String type);
}

Is it possible to create 2 session factories and use them on some conditions?

Also, if i will have different logic for directories on first and second SFTP servers, do i need two SftpMessageHandlers and MessagingGateways?

Now i think as solution to create two SftpService interfaces, two packs of configs and in application logic change where to upload.


Solution

  • Solved by creating second

    @Bean
      public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
        final DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost(properties.getHost());
        factory.setPort(properties.getPort());
        factory.setUser(properties.getUser());
        factory.setPassword(properties.getPassword());
        factory.setAllowUnknownKeys(true);
        return factory;
      }
    

    For second sftp connection, and implemented second

    @Bean
      @ServiceActivator(inputChannel = "toSftpChannel")
      public MessageHandler handler(@Value("'${sftp.folder}'.concat(headers['type'])")
                                      final String remoteDirectory) {
        final SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
        handler.setRemoteDirectoryExpressionString(remoteDirectory);
        handler.setAutoCreateDirectory(true);
        return handler;
      }
    
    @MessagingGateway
    public interface SftpService {
    
      @Gateway(requestChannel = "toSftpChannel")
      void uploadFileToSftp(@Payload File file,
                            @Header("filename") String filename,
                            @Header("type") String type);
    }
    

    with different inputChannel name