在Spring集成中处理多个SFTP主机
我想配置多个SFTP主机,以便基于用户输入(用户传递hostName),我将选择HOST并执行读取操作。
问题是我不想创建多个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-Boot / Spring-Integration中实现!!!
答案 0 :(得分: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