rellocs wood
rellocs wood

Reputation: 1471

spring integration : solutions/tips on connect multiple sftp server?

My spring batch project needs to download files from multiple sftp servers. the sftp host/port/filePath is config in application.properties file. I consider using the spring integration 'sftp out-bound gateway' to connect these servers and download files. but Im don't how to do this kind of configuration(I'm using java config, ) and make it work? i guess I need some way to define multiple session factory according to the number of sftp server info config in application.properties file.

properties file:

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

config class:

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

Upvotes: 1

Views: 3283

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

Right, the server is specified in the session factory, not the gateway. The framework does provide a delegating session factory, allowing it to be selected from one of the configured factories for each message sent to the gateway. See Delegating Session Factory.

EDIT

Here's an example:

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

    }

}

with properties...

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

Upvotes: 4

Related Questions