【问题标题】:how dynamic create ftp adapter in spring integration?spring 集成中如何动态创建 ftp 适配器?
【发布时间】:2016-05-25 03:36:09
【问题描述】:

感谢关注
我在我的项目中使用了 spring 集成,我想从多个具有不同地址的 ftp 服务器检索许多输入文件,如下图所示:

如何在我的项目中动态创建inbound-adapter 以从服务器轮询和检索文件?

【问题讨论】:

    标签: java spring spring-integration spring-batch esb


    【解决方案1】:

    如果您被允许在您的项目中使用非“通用”(GA) 版本的 3rd 方库(例如候选版本 (RC) 或里程碑 (M)),那么您可以使用版本 5.0.0.M2弹簧集成。它是截至 2017 年 3 月 9 日的最新 published 版本。

    5.0开始,Spring Integration 包含Java DSL Runtime 流注册功能。它允许您定义集成流(包括入站适配器),就像在标准 bean 中一样,但它可以在任何运行时完成。

    您只需要使用这 3 个步骤:

    1. 从 Spring 上下文中获取 IntegrationFlowContext bean,例如通过自动装配:
      @Autowired
      public MyClass(IntegrationFlowContext flowContext) {
        this.flowContext = flowContext;
      }
    
    1. 用它构建新的流程,例如:
      IntegrationFlowRegistration registration = flowContext
          .registration(IntegrationFlows   // this method accepts IntegrationFlow instance
                            .from(s -> s.ftp(ftpSessionFactory())
                                        .localFilter(localFileFilter())
                            //other actions
                            .get())        // standard end of DSL flow building process
          .autoStartup(true)               // not required but can be useful
          .register();                     // this makes the flow exist in the context
    
    1. 当需要删除动态创建的流程时,只需使用您在上一步中获得的注册 ID 再次引用 IntegrationFlowContext
    // retrieve registration ID from the object created above
    String dynamicFlowRegistrationId = registration.getId();
    // the following will also gracefully stop all the processes within the flow
    flowContext.remove(dynamicFlowRegistrationId);
    

    GitHub 上还有一个DynamicTcpClient sample

    【讨论】:

    • 动态流注册在 1.2 DSL 版本initial milestone blog here 中也可用;当前版本是 1.2.1。您无需等待 5.0(2017 年夏季到期)。
    【解决方案2】:

    请参阅dynamic-ftp sample。虽然它只涵盖出站端,但 README 中有一些链接可以讨论入站端需要做什么(将每个适配器放在子上下文中,将消息发送到主上下文中的通道)。

    另请参阅我对similar question for multiple IMAP mail adapters using Java configurationfollow-up question 的回答。

    你应该能够使用那里使用的技术。

    【讨论】:

    • 感谢@Gary 的回复,这是在上下文中创建动态 ftp 适配器的好解决方案。
    • 有 dsl 的例子
    • 使用Dynamic and Runtime Integration Flows。有关示例,请参见下面的 @Toparvion 的答案。
    猜你喜欢
    • 2022-01-16
    • 2020-12-15
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多