【发布时间】:2016-11-29 08:34:10
【问题描述】:
我有一个 Web 项目,我正在使用 Spring Integration 通过 FTP 在远程目录上上传文件。但是,FTP 属性是动态加载的(从数据库中),每个请求的属性可能不同。幼稚的做法:
初始创建DefaultFtpSessionFactory bean:
@Bean
public DefaultFtpSessionFactory defaultFtpSessionFactory() {
return new DefaultFtpSessionFactory();
}
IntegrationFlow豆:
@Bean
public IntegrationFlow integrationFlow(DefaultFtpSessionFactory defaultFtpSessionFactory) {
// Flow config
}
将此bean注入控制器并设置属性:
@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 单例)。那么,我怎样才能以安全的方式实现这一点呢?
【问题讨论】:
-
您可以做的是创建一个包含您的 ftp 有效负载和会话属性的消息,您可以使用这些属性在流中更新您的 sessionFactory。
标签: java spring spring-mvc spring-integration