Apache Commons NET: Should I create a new FTPClient object for each connection or reuse it?

I'm just wondering: in my Java application, every time I need to connect to an FTP server, do I have to create a new FTPClient object or create one FTPClient () object and reuse it every time I connect to an FTP server?

+8
java performance android ftp-client apache-commons-net
source share
3 answers

Reuse will be better since you will not need a new instance with every new connection, even to a new server. Because connect and disconnect methods can do the job for you.

So reuse as many objects as you can.

+3
source share

Both will work, but the commons.net apache libraries are not thread safe , so if you use the same client with multiple threads, keep in mind that you will need to synchronize access to it.

+11
source share

Not knowing exactly what your code should do (how often it connects, how many files it needs to transfer, etc.), it's hard to say.

My personal opinion would be one FTPClient() , but connect / authenticate / transmit / disconnect every time you need to do something (provided there is a reasonable gap between them). The reason is that FTP sessions often have rather short idle timeouts.

+3
source share

All Articles