2016-11-29 30 views
1

我有一个Web项目,我正在使用Spring集成通过FTP在远程目录上进行文件上传。但是,FTP属性是动态加载的(来自数据库),并且它们对于每个请求都可能不同。原始的方法:春季集成上传与动态FTP属性

最初创建DefaultFtpSessionFactory豆:

@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); 
} 

当然,这由于存在竞争条件(两个请求者),所以这是一个坏主意sts可以在同一时间访问DefaultFtpSessionFactory单身人士)。那么,我怎样才能以安全的方式实现这一目标呢?

+0

您可以做的是创建一个消息,其中包含您的ftp负载和会话属性,您可以使用它更新流中的sessionFactory。 – JEY

回答

1

动态注册流程的最后一部分 - 请参阅the blog introducing the feature;也许将这些流保存在缓存中。

有关我们创建多个tcp客户端适配器并缓存输入通道的示例,请参见dynamic-tcp-client;对ftp使用类似的技术 - 还有一个旧版本的dynamic-ftp,它预先记录了DSL和动态流量注册。

+0

自从我使用Spring 4以来,我看了一下动态FTP示例。据我所知,对于每个新的'MessageChannel',您都要创建一个新的'ApplicationContext'并缓存它。是否有可能避免创建上下文?另外,为了使所有的工作都能正常工作,应用程序启动时不应该自动布线bean。这使得事情非常不方便,因为配置类/ XML必须超出'@ ComponentScan'范围。 –

+0

动态流方法(用在我引用的tcp示例中)不会创建子上下文。您还可以使用[委托会话工厂](http://docs.spring.io/spring-integration/reference/html/ftp.html#ftp-dsf),其中实际的连接/会话从“ThreadLocal”确定。 –