如何在春季 sftp 入站通道适配器中配置委派会话工厂



我们希望在运行时将会话工厂委托给 spring sftp 入站通道适配器。为此,我们完成了以下配置。

我们已经浏览了 spring-sftp 集成文档,但我们不确定如何通过 java 设置会话工厂属性值。您能否建议我们如何使用委派会话工厂在春季 sftp 入站通道适配器中委派会话工厂运行时。

XML 配置

<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代码

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();

委派会话工厂实际上是为出站适配器和网关而设计的。通常,入站适配器不会切换到不同的服务器。

像这样在main线程上设置线程键不会执行任何操作。

您需要在调用适配器的线程上设置/清除密钥;这在文档中针对出站适配器进行了显示。

对于入站适配器,您需要在轮询器线程上执行此操作。

我不知道您将使用什么标准来选择工厂,但您可以使用智能轮询器来做到这一点。

相关内容

最新更新