2
votes

We want to delegate the session-factory at run time to spring sftp inbound channel adapter. For that we have done the below configuration.

We have gone through the spring-sftp integration docs but we are not sure how to set the session-factory attribute value via java. Could you please suggest us how to delegate the session-factory run time in spring sftp inbound channel adapter using Delegating Session Factory.

XML Configuration

<beans>
<bean id="defaultSftpSessionFactoryOne" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
    <property name="host" value="**.***.**.***" />
    <property name="port" value="**" />
    <property name="user" value="######" />
    <property name="password" value="######" />
    <property name="allowUnknownKeys" value="true" />
</bean>

<bean id="defaultSftpSessionFactoryTwo"     class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
    <property name="host" value="**.***.**.***" />
    <property name="port" value="**" />
    <property name="user" value="######" />
    <property name="password" value="######" />
    <property name="allowUnknownKeys" value="true" />
</bean>

<bean id="delegatingSessionFactory"     class="org.springframework.integration.file.remote.session.DelegatingSessionFactory">
    <constructor-arg>
        <bean id="factoryLocator"
            class="org.springframework.integration.file.remote.session.DefaultSessionFactoryLocator">
            <constructor-arg name="factories">
                <map>
                    <entry key="one" value-ref="defaultSftpSessionFactoryOne"></entry>
                    <entry key="two" value-ref="defaultSftpSessionFactoryTwo"></entry>
                </map>
            </constructor-arg>
        </bean>
    </constructor-arg>
</bean>

<int:channel id="receiveChannel" />

<int-sftp:inbound-channel-adapter id="sftpInbondAdapter" auto-startup="false"
    channel="receiveChannel" session-factory="delegatingSessionFactory"
    local-directory="C:\\Users\\sftp" remote-directory="/tmp/archive"
    auto-create-local-directory="true" delete-remote-files="false"
    filename-regex=".*\.txt$">
    <int:poller cron="0/10 * * * * ?">
    </int:poller>
</int-sftp:inbound-channel-adapter>

java code

ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
DelegatingSessionFactory<String> dsf = (DelegatingSessionFactory<String>) ac.getBean("delegatingSessionFactory");
SessionFactory<String> one = dsf.getFactoryLocator().getSessionFactory("one");
SessionFactory<String> two = dsf.getFactoryLocator().getSessionFactory("two");
dsf.setThreadKey("two");
SourcePollingChannelAdapter spca = (SourcePollingChannelAdapter) ac.getBean("sftpInbondAdapter");
spca.start();
1
Add java tag to questionFlatlineato

1 Answers

1
votes

The delegating session factory was really intended for outbound adapters and gateways. Typically, inbound adapters don't switch to different servers.

Setting the thread key on the main thread like that does nothing.

You need to set/clear the key on the thread that invokes the adapter; this is shown for outbound adapters in the documentation.

For inbound adapters you need to do it on the poller thread.

I don't know what criteria you will use to select the factory, but you can use a smart poller to do it.