Molay
Molay

Reputation: 1304

Spring integration - sftp:outbound-channel-adapter - dynamic sftpSessionFactory?

How to make sftpSessionFactory dynamic based on the file? For example, if filename starts with A, this file has to be placed on a particular SFTP location, for B, C, D type files has their own SFTP locations. So I have 4 different host/user/password/port, as an example, I picked up 4, but I may have more than 20 type of files. I can't hardcode host/user/password/port values in application.properties file or in integration.xml file.

On spring boot server startup, I get all those sftp configuration details from config server as a Map.

Map<String, SftpValues> values = getAllSftpValues(); // this connects to a config server to fetch all type of sftp details.
SftpValues sftpValues = values.get("A");

sftpValues ==> these values i should be able to set into DefaultSftpSessionFactory dynamically and pass sftpSessionFactory on to outbound-channel-adapter for each and every file.

<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
    <property name="host" value="localhost"/>
    <property name="user" value="user01"/>
    <property name="password" value="abc123"/>
    <property name="port" value="990"/>
</bean>

<int:channel id="sftpChannel"/>

<file:inbound-channel-adapter directory="#{T(System).getProperty('java.io.tmpdir')}" id="fileInbound"
                              channel="sftpChannel" filename-pattern="*.xml">
    <int:poller fixed-rate="1000" max-messages-per-poll="100"/>
</file:inbound-channel-adapter>

<int-sftp:outbound-channel-adapter id="sftpOutboundAdapter" session-factory="sftpSessionFactory"
                                   channel="sftpChannel" charset="UTF-8" remote-directory="/"
                                   remote-file-separator="/"/>

Upvotes: 0

Views: 344

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121177

set into DefaultSftpSessionFactory dynamically

That's not correct. You cannot mutate the session factory. What you can do is to utilize a DelegatingSessionFactory approach.

You configure it for some set of delegates: public DelegatingSessionFactory(Map<Object, SessionFactory<F>> factories, SessionFactory<F> defaultFactory) {. And then you call its public Message<?> setThreadKey(Message<?> message, Object key) { before sending to the <int-sftp:outbound-channel-adapter> and then its public Message<?> clearThreadKey(Message<?> message) { after or as a second subscriber of the publish-subscribe-channel for that sftpOutboundAdapter.

See more in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/ftp.html#ftp-dsf

Upvotes: 1

Related Questions