我有一个 Web 项目,我在其中使用 Spring Integration 通过 FTP 将文件上传到远程目录。但是,FTP 属性是动态加载的(从数据库),每个请求的属性可能不同。天真的方法:
最初创建 DefaultFtpSessionFactory
bean:
@Bean
public DefaultFtpSessionFactory defaultFtpSessionFactory() {
return new DefaultFtpSessionFactory();
}
IntegrationFlow
bean :
@Bean
public IntegrationFlow integrationFlow(DefaultFtpSessionFactory defaultFtpSessionFactory) {
// Flow config
}
将这个 bean 注入(inject) Controller 并设置属性:
@Autowired
private DefaultFtpSessionFactory defaultFtpSessionFactory;
@Autowired
private FtpConfigService ftpConfigService;
@RequestMapping(value = "upload", method = RequestMethod.GET)
public RequestEntity<String> upload() {
defaultFtpSessionFactory.setHost(ftpConfigService.getHost());
// Set other properties
// ... and upload file
return new RequestEntity<>(HttpStatus.OK);
}
当然,这是个坏主意,因为存在竞争条件(两个请求可以同时访问 DefaultFtpSessionFactory
单例)。那么,我怎样才能以安全的方式实现这一目标呢?
最佳答案
动态注册流程的最后部分 - 请参阅 the blog introducing the feature ;也许将这些流保存在缓存中。
参见 dynamic-tcp-client例如,我们动态创建多个 tcp 客户端适配器并缓存输入 channel ;对 ftp 使用类似的技术 - 还有一个旧示例 dynamic-ftp它早于 DSL 和动态流注册。
关于java - 具有动态 FTP 属性的 Spring Integration 上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40861112/