Java 具有动态FTP属性的Spring集成上传
我有一个web项目,我使用SpringIntegration通过FTP在远程目录上上传文件。但是,FTP属性是动态加载的(从数据库中),对于每个请求,它们可能不同。天真的方法: 最初创建Java 具有动态FTP属性的Spring集成上传,java,spring,spring-mvc,spring-integration,Java,Spring,Spring Mvc,Spring Integration,我有一个web项目,我使用SpringIntegration通过FTP在远程目录上上传文件。但是,FTP属性是动态加载的(从数据库中),对于每个请求,它们可能不同。天真的方法: 最初创建DefaultFtpSessionFactorybean: @Bean public DefaultFtpSessionFactory defaultFtpSessionFactory() { return new DefaultFtpSessionFactory(); } @Bean public I
DefaultFtpSessionFactory
bean:
@Bean
public DefaultFtpSessionFactory defaultFtpSessionFactory() {
return new DefaultFtpSessionFactory();
}
@Bean
public IntegrationFlow integrationFlow(DefaultFtpSessionFactory defaultFtpSessionFactory) {
// Flow config
}
IntegrationFlow
bean:
@Bean
public DefaultFtpSessionFactory defaultFtpSessionFactory() {
return new DefaultFtpSessionFactory();
}
@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);
}
@Autowired
私有DefaultFtpSessionFactory DefaultFtpSessionFactory;
@自动连线
私有FtpConfigService FtpConfigService;
@RequestMapping(value=“upload”,method=RequestMethod.GET)
公共请求实体上载(){
defaultFtpSessionFactory.setHost(ftpConfigService.getHost());
//设置其他属性
//…并上传文件
返回新的RequestEntity(HttpStatus.OK);
}
当然,这是一个坏主意,因为存在竞争条件(两个请求可以同时访问
DefaultFtpSessionFactory
singleton)。那么,如何以安全的方式实现这一点呢?动态注册流的最终部分-请参阅;也许可以将这些流保存在缓存中
有关动态创建多个tcp客户端适配器并缓存输入通道的示例,请参见;对ftp使用类似的技术-还有一个较早的示例,其日期早于DSL和动态流注册。您可以做的是创建一条包含ftp负载和会话属性的消息,您可以使用该消息更新流中的sessionFactory。在使用春天4。据我所知,对于每个新的
MessageChannel
,您都在创建一个新的ApplicationContext
,并将其缓存。是否有可能避免创建上下文?另外,为了使一切正常工作,不应该在应用程序启动时自动连接bean。这使得事情变得非常不方便,因为配置类/XML必须保持在@ComponentScan
范围之外。动态流方法(在我引用的tcp示例中使用)不会创建子上下文。您还可以使用,其中实际连接/会话由ThreadLocal
确定。