【问题标题】:Spring Integration upload with dynamic FTP properties具有动态 FTP 属性的 Spring Integration 上传
【发布时间】: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


【解决方案1】:

动态注册流程的最后部分——见the blog introducing the feature;也许将这些流保存在缓存中。

查看dynamic-tcp-client 的示例,我们动态创建多个 tcp 客户端适配器并缓存输入通道;对 ftp 使用类似的技术 - dynamic-ftp 也有一个较旧的示例,它早于 DSL 和动态流注册。

【讨论】:

  • 自从我使用 Spring 4 以来,我查看了您的动态 FTP 示例。据我所知,对于每个新的 MessageChannel,您正在创建一个新的 ApplicationContext 并缓存它。是否可以避免创建上下文?另外,为了使一切正常运行,不应在应用程序启动时自动装配任何 bean。这让事情变得非常不方便,因为配置类/XML 必须不在@ComponentScan 范围内。
  • 动态流方法(在我引用的 tcp 示例中使用)不会创建子上下文。您还可以使用Delegating Session Factory,其中实际连接/会话由ThreadLocal 确定。
猜你喜欢
  • 2017-01-22
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
  • 2016-04-26
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多