[[ftp-dsf]]
= Delegating Session Factory
:page-section-summary-toc: 1

Version 4.2 introduced the `DelegatingSessionFactory`, which allows the selection of the actual session factory at runtime.
Prior to invoking the FTP endpoint, 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.

See xref:handler-advice/context-holder.adoc[Context Holder Advice] for more information how this factory can be used together with a `ContextHolderRequestHandlerAdvice`.

IMPORTANT: When you use session caching (see xref:ftp/session-caching.adoc[FTP Session Caching]), each of the delegates should be cached.
You cannot cache the `DelegatingSessionFactory` itself.

Starting with version 5.0.7, the `DelegatingSessionFactory` can be used in conjunction with a `RotatingServerAdvice` to poll multiple servers; see xref:ftp/rotating-server-advice.adoc[Inbound Channel Adapters: Polling Multiple Servers and Directories].

