SFTP Session Caching
从 Spring Integration 3.0 版本开始,默认情况下不再缓存会话。cache-sessions
属性不再支持端点。如果您希望缓存会话,您必须使用 CachingSessionFactory
(参见下一个示例)。
Starting with Spring Integration version 3.0, sessions are no longer cached by default.
The cache-sessions
attribute is no longer supported on endpoints.
If you wish to cache sessions, you must use a CachingSessionFactory
(see the next example).
在 3.0 之前的版本中,会话默认自动缓存。可以设置 cache-session
属性来禁用自动缓存,但该解决方案无法为其他会话缓存属性配置其它选项。例如,无法限制创建的会话数量。为了满足此要求和其他配置选项,我们添加了 CachingSessionFactory
。它提供了 sessionCacheSize
和 sessionWaitTimeout
属性。顾名思义,sessionCacheSize
属性控制工厂在缓存中维护的活动会话数量(默认为无限)。如果达到了 sessionCacheSize
阈值,则获取另一个会话的任何尝试都会被阻止,直到缓存会话之一可用或直到会话的等待时间到期(默认的等待时间为 Integer.MAX_VALUE
)。sessionWaitTimeout
属性启用等待时间的配置。
In versions prior to 3.0, the sessions were automatically cached by default.
A cache-sessions
attribute was available for disabling the auto caching, but that solution did not provide a way to configure other session-caching attributes.
For example, you could not limit on the number of sessions created.
To support that requirement and other configuration options, we added a CachingSessionFactory
.
It provides sessionCacheSize
and sessionWaitTimeout
properties.
As its name suggests, the sessionCacheSize
property controls how many active sessions the factory maintains in its cache (the default is unbounded).
If the sessionCacheSize
threshold has been reached, any attempt to acquire another session blocks until either one of the cached sessions becomes available or until the wait time for a session expires (the default wait time is Integer.MAX_VALUE
).
The sessionWaitTimeout
property enables configuration of the wait time.
如果要缓存你的会话,请配置你的默认会话工厂(如前所述),然后将其包装在一个 CachingSessionFactory
实例中,你可以在其中提供这些附加属性。以下示例介绍了如何执行此操作:
If you want your sessions to be cached, configure your default session factory (as described earlier) and then wrap it in an instance of CachingSessionFactory
where you may provide those additional properties.
The following example shows how to do so:
<bean id="sftpSessionFactory"
class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="localhost"/>
</bean>
<bean id="cachingSessionFactory"
class="org.springframework.integration.file.remote.session.CachingSessionFactory">
<constructor-arg ref="sftpSessionFactory"/>
<constructor-arg value="10"/>
<property name="sessionWaitTimeout" value="1000"/>
</bean>
前面的示例创建了一个 CachingSessionFactory
,其 sessionCacheSize
设置为 10
,其 sessionWaitTimeout
设置为 1 秒(1000 毫秒)。
The preceding example creates a CachingSessionFactory
with its sessionCacheSize
set to 10
and its sessionWaitTimeout
set to one second (1000 milliseconds).
从 Spring Integration 3.0 版本开始,CachingConnectionFactory
提供了一个 resetCache()
方法。调用时,所有空闲会话都会立即关闭,而正在使用的会话则在返回缓存时关闭。在使用 isSharedSession=true
时,只有当最后一个通道关闭时,通道才会关闭并且共享会话才会关闭。对会话的新请求会根据需要建立新的会话。
Starting with Spring Integration version 3.0, the CachingConnectionFactory
provides a resetCache()
method.
When invoked, all idle sessions are immediately closed and in-use sessions are closed when they are returned to the cache.
When using isSharedSession=true
, the channel is closed and the shared session is closed only when the last channel is closed.
New requests for sessions establish new sessions as necessary.
从 5.1 版本开始,CachingSessionFactory
有一个新属性 testSession
。当为真时,会话将通过对空路径执行 REALPATH
命令来进行测试以确保其仍处于活动状态;否则,它将从缓存中删除;如果缓存中没有活动会话,则创建一个新会话。
Starting with version 5.1, the CachingSessionFactory
has a new property testSession
.
When true, the session will be tested by performing a REALPATH
command for an empty path to ensure it is still active; if not, it will be removed from the cache; a new session is created if no active sessions are in the cache.