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

不想你离开。 提交于 2019-12-30 08:24:06

问题


I'm just wondering: in my Java application, each time I need to connect to an FTP server, should I create a new FTPClient object, or should I create one FTPClient() object and re-use that each time I connect to an FTP server?


回答1:


Reusing would be better since you wont need a new instance every time you are creating a new connection, even to a new server. Because the connect and disconnect methods can do the job for you.

So reuse as many objects as you can.




回答2:


Both will work, but apache commons.net libraries are not thread safe, so if you do use one client with multiple threads, be aware that you'll have to synchronize access to it.




回答3:


Without knowing exactly what your code has to do (how often it connects, how many files it needs to transfer etc) it's difficult to say.

My personal opinion would be one FTPClient() but connect/authenticate/transfer/disconnect each time you need to do something (assuming there will be a reasonable gap in between). The reasoning is that FTP sessions often have fairly short inactivity time-outs.



来源:https://stackoverflow.com/questions/6892066/apache-commons-net-should-i-create-a-new-ftpclient-object-on-each-connection-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!