问题描述
我的spring批处理项目需要从多个sftp服务器下载文件.sftp 主机/端口/文件路径是 application.properties 文件中的配置.我考虑使用 spring 集成sftp 出站网关"来连接这些服务器并下载文件.但我不知道如何进行这种配置(我正在使用 java config,)并使其工作?我想我需要一些方法来根据 application.properties 文件中 sftp 服务器信息配置的数量定义多个会话工厂.
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.
属性文件:
sftp.host=host1,host2
sftp.user=user1,user2
sftp.pwd=pwd1,pwd2
配置类:
@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();
}
推荐答案
对了,服务器是在会话工厂中指定的,而不是网关.该框架确实提供了一个委托会话工厂,允许从发送到网关的每条消息的配置工厂之一中选择它.请参阅委托会话工厂.
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.
编辑
这是一个例子:
@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);
}
}
有属性...
sftp.name=one,two
sftp.host=host1,host2
sftp.user=user1,user2
sftp.pwd=pwd1,pwd2
这篇关于spring 集成:连接多个 sftp 服务器的解决方案/提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!