java - 具有动态 FTP 属性的 Spring Integration 上传

标签 java spring spring-mvc spring-integration

我有一个 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/

相关文章:

java - Android:WAITING对象出现

java - AggregatingMessageHandler 的手动 ACK

java - 如何在Spring MVC中更新静态资源而不重建?

java - 使用 Spring MVC 3.1+ WebApplicationInitializer 以编程方式配置 session-config 和 error-page

java - Junit 5 无法解析方法的参数

java - 为什么JVM CMS(concurrent mark-and-sweep) GC需要两次停顿?

Java Codelab While 语句

java - 条件注释上的 CacheEvict - Spring Caching

spring - 带逗号的假调用 URL

SpringMVC ApplicationContext 层次结构