Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 471
Sri Gandhi Avatar asked Oct 21 '22 16:10

Sri Gandhi


2 Answers

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 ...

like image 51
Philip Whitehouse Avatar answered Oct 24 '22 11:10

Philip Whitehouse


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

like image 26
Honwhy Wang Avatar answered Oct 24 '22 10:10

Honwhy Wang