如何在spring集成中动态创建ftp适配器?
谢谢你的关注
我在我的项目中使用了spring集成,我想从多个ftp服务器检索许多输入文件,其中不同的地址如下图所示:
如何在我的项目中创建动态inbound-adapter
来轮询和检索服务器中的文件?
请参阅dynamic-ftp示例 。 虽然它只涵盖出站端,但README中有链接可以讨论入站端需要做什么(将每个适配器放在子上下文中,将消息发送到主上下文中的通道)。
另请参阅我对使用Java配置的多个IMAP邮件适配器的类似问题的回答,然后是后续问题 。
您应该能够使用那里使用的技术。
如果您允许在项目中使用第三方库的非“一般可用性”(GA)版本(例如,候选版本(RC)或里程碑(M)),那么您可以使用Spring的5.0.0.M2
版本积分。 这是截至2017年3月9日的最新发布版本。
从5.0
开始,Spring Integration包括Java DSL Runtime流注册function 。 它允许您定义集成流程(包括入站适配器),就像在标准bean中一样,但它可以在任何运行时刻完成。
所有你需要使用它是这3个步骤:
- 从Spring上下文获取
IntegrationFlowContext
bean,例如通过自动assembly:
@Autowired public MyClass(IntegrationFlowContext flowContext) { this.flowContext = flowContext; }
- 用它构建新的流程,例如:
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
- 在删除动态创建的流时,只需再次使用您在上一步中获得的注册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示例 。