Search code examples
springspring-bootspring-integrationsftpspring-integration-sftp

Spring Integration CachingSessionFactory: What does setTestSession(true) do?


I'm working with the CachingSessionFactory and I was wondering what the property `setTestSession(boolean testSession) does and when to use it?

The docs are not telling a lot about what it does: https://docs.spring.io/spring-integration/api/org/springframework/integration/file/remote/session/CachingSessionFactory.html#setTestSession(boolean)


Solution

  • See SFTP docs: https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-session-caching

    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.

    The logic there in the cache is like this:

    public boolean isStale(Session<F> session) {
                return CachingSessionFactory.this.testSession ? !session.test() : !session.isOpen();
            }
    

    See SftpSession for implementation details.