1

I need to build a standalone api using Spring integration's sftp adapter, but the caller will pass in the sftp connection parameters (host, user, pwd, etc..) and so I cannot initialize them in the spring context xml. I'm looking for advice on the best way to do with out needing to teardown and recreate an application context with each invocation. Here's my context xml right now, and i'd like to externalize the DefaultSftpSessionFactory parameters.

<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
    <property name="host" value="${host}" />
    <property name="port" value="${serverport}" />
    <property name="user" value="${username}" />
    <property name="password" value="${password}" />
    <property name="allowUnknownKeys" value="true" />
</bean>
<int:channel id="inputChannel" />
<int-sftp:outbound-channel-adapter id="sftpOutboundAdapter"
    session-factory="sftpSessionFactory"
    channel="inputChannel"
    charset="UTF-8"
    remote-file-separator="/"
    remote-directory="/accounts/12026622/Reports/" 
    use-temporary-file-name="false"
    mode="REPLACE"
    remote-filename-generator-expression="payload.getName() + '-foo'"      />       
javakart
  • 43
  • 7

1 Answers1

0

See the dynamic ftp sample app.

It uses a dynamic router to create a new mini (parameterized) application context for each new destination and caches them for reuse.

It's pretty straightforward; the sample uses XML; if you prefer Java configuration, this answer and its follow-up uses a similar technique for inbound mail adapters.

If you don't want to use this technique for some reason, another option would be to use the DelegatingSessionFactory with a custom SessionFactoryLocator which creates the session factories on the fly.

Community
  • 1
  • 1
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Also see this https://spring.io/blog/2016/07/08/java-dsl-for-spring-integration-1-2-m1-and-1-1-3-are-available. Now you can configure the whole flow at runtime. Just exactly like you would like for FTP – Artem Bilan Jul 30 '16 at 16:05
  • Gary - is there an example of DelegatingSessionFactory with SessionFactoryLocator ? – javakart Aug 02 '16 at 15:44
  • Not that I am aware of but it shouldn't be difficult to add the host/port/credentials to a message header and use them in a factory locator. I am at the SpringOne Platform conference this week; I could create an example next week if you remind me by adding a comment here then. – Gary Russell Aug 02 '16 at 18:30