Sri Gandhi
Sri Gandhi

Reputation: 123

FTPClient Pool - Java

I am writing a Rest Service that connects to an FTP server to read some files, then do some operations over the read data to serve the service request. I am using Apache commons FTPClient.

As a temporary solution, I am creating an FTPClient object - then connecting it - and then logging in with the credentials - inside a method (the client is local to this method - doing this as FTPClient is not thread safe) in my data access layer and then disconnecting it before coming out of the methods(ie.. after reading the file). The issue is, FTPClient is taking some 3-7 seconds for logging in which is very high. So I am thinking of implementing an FTPClientPool that can provide an already prepared client in the data access method.

Do any such ClientPools already exist?

If yes, then what one should I opt for?

If no, the difficulty in implementing is once created and connected, How long does an apache FTPClient stay alive? for infinite time?? (what I mean is what is the default keep alive time for an FTPClient - idle time after which client gets disconnected - coz I see various kind of times in the java docs. :( ) And next questions is How do you keep it alive always?? (may be sending the NOOPS after regular intervals in a separate thread??) Any kind of help regarding how should I move forward is really helpful.

Thanks & Regards

Upvotes: 5

Views: 5787

Answers (3)

Eyal Sooliman
Eyal Sooliman

Reputation: 2258

Configure

protected static ThreadLocal<FTPClient> ftpClientContainer = new 
ThreadLocal<>();

Then use:

//login() will be your login method to FTP:

ftpClientContainer.set(ftpLogin()); 

Then in each method add:

FTPClient ftpClient = ftpClientContainer.get();

and finely when done:

//ftpDisconnect () will be your disconnect method to FTP:

ftpDisconnect(ftpClientContainer.get());

Upvotes: 0

Honwhy Wang
Honwhy Wang

Reputation: 118

As for ClientPools, I happened to write a demo project. commons-pool-ftp

I am getting a little bit annoyed by the ftp protocol, in our experience, it would meet broken pipe when testing on the client that just getting from the pool.

testOnBorrow=true

Upvotes: 1

Philip Whitehouse
Philip Whitehouse

Reputation: 4157

Idle timeout for clients is generally determined server side.

Here's some of the more non obvious client parameters:

  • soTimeout - Determines how long the client blocks waiting for a message. Generally you poll a socket every so often and this determines how long you wait during a poll.
  • soLinger - Determines how long to keep the connection after close() has been called.

From my experience of using FTP, they normally just reconnect if the connection closes - it's not normally vital to have a constant uninterrupted connection unlike in other applications.

What are you using FTP for - it's normally not that time critical a service ...

Upvotes: 2

Related Questions