Delegating Session Factory

版本 4.2 引入了 DelegatingSessionFactory,允许在运行时选择实际会话工厂。在调用 SFTP 端点之前,可以在工厂上调用 setThreadKey() 以将键与当前线程相关联。然后使用该键查找要使用的实际会话工厂。使用后,可以通过调用 clearThreadKey() 清除该键。

Version 4.2 introduced the DelegatingSessionFactory, which allows the selection of the actual session factory at runtime. Prior to invoking the SFTP endpoint, you can call setThreadKey() on the factory to associate a key with the current thread. That key is then used to look up the actual session factory to be used. You can clear the key by calling clearThreadKey() after use.

我们添加了便捷方法,以便你可以更轻松地从消息流中执行此操作,如下面的示例所示:

We added convenience methods so that you can more easily do so from a message flow, as the following example shows:

<bean id="dsf" class="org.springframework.integration.file.remote.session.DelegatingSessionFactory">
    <constructor-arg>
        <bean class="o.s.i.file.remote.session.DefaultSessionFactoryLocator">
            <!-- delegate factories here -->
        </bean>
    </constructor-arg>
</bean>

<int:service-activator input-channel="in" output-channel="c1"
        expression="@dsf.setThreadKey(#root, headers['factoryToUse'])" />

<int-sftp:outbound-gateway request-channel="c1" reply-channel="c2" ... />

<int:service-activator input-channel="c2" output-channel="out"
        expression="@dsf.clearThreadKey(#root)" />

在使用会话缓存(参阅 SFTP Session Caching)的时候,应该缓存每个委托。您无法缓存 DelegatingSessionFactory 本身。

When using session caching (see SFTP Session Caching), each of the delegates should be cached. You cannot cache the DelegatingSessionFactory itself.

从5.0.7版本开始,`DelegatingSessionFactory`可以与`RotatingServerAdvice`结合使用来轮询多个服务器;请参阅Inbound Channel Adapters: Polling Multiple Servers and Directories

Starting with version 5.0.7, the DelegatingSessionFactory can be used in conjunction with a RotatingServerAdvice to poll multiple servers; see Inbound Channel Adapters: Polling Multiple Servers and Directories.