how dynamic create ftp adapter in spring integration?

自闭症网瘾萝莉.ら 提交于 2019-12-28 06:52:33

问题


Thanks for attention
i used spring integration in my project, i want to retrieve many input file from multiple ftp server with different address as bellow image:

how to create dynamically inbound-adapter in my project to polling and retrieve files from servers?


回答1:


See the dynamic-ftp sample. While it only covers the outbound side, there are links in the README to discussions about what needs to be done on the inbound side (put each adapter in a child context that send messages to a channel in the main context).

Also see my answer to a similar question for multiple IMAP mail adapters using Java configuration and then a follow-up question.

You should be able to use the technique used there.




回答2:


If you're allowed to use non-"General Availability" (GA) versions of 3rd party libraries (e.g. release candidates (RC) or milestones (M)) in your project, then you can utilize version 5.0.0.M2 of Spring Integration. It is the latest published version as of Mar 09 '17.

Starting from 5.0, Spring Integration includes Java DSL Runtime flow registration feature. It allows you to define integration flows (including inbound adapters) just like you do it in standard beans but it can be done at any runtime moment.

All you need to use it are these 3 steps:

  1. Get IntegrationFlowContext bean from Spring context, e.g. by means of autowiring:
  @Autowired
  public MyClass(IntegrationFlowContext flowContext) {
    this.flowContext = flowContext;
  }
  1. Build new flow with it, for example:
  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. When it's time to remove dynamically created flow, just refer to IntegrationFlowContext again with registration id you've got in the previous step:
// 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);

There is also a DynamicTcpClient sample available on GitHub.



来源:https://stackoverflow.com/questions/35388816/how-dynamic-create-ftp-adapter-in-spring-integration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!