Setting connection timeout for FtpClient

When using ftpClient.connect with an existing host that does not have ftp service, the timeout occurs only after 5 minutes, which is too much.

I tried to set various timeouts (setDefaultTimeout, setDataTimeout) that did not change anything.

FtpClient inherits from SocketClient , which has the setConnectTiemout method, but when I use it, I get java.lang.NoSuchMethodError: org/apache/commons/net/ftp/FTPClient.setConnectTimeout when it starts. This seems to be due to some J2SW 1.2 compatibility, as described in the Commons-net FAQ:

Q: How to set the connection timeout? http://wiki.apache.org/commons/Net/FrequentlyAskedQuestions

They propose to implement their own SocketFactory object to create objects from the extended Socket class using a specific timeout. However, when I try to use ftpClient.setSocketFactory I also get java.lang.NoSuchMethodError .

Any help how to reduce connection timeout?

+4
source share
3 answers

Although there is a possible solution for this older version of the Commons Net library, I suggest finding out why the wrong version of Commons Net is being used. To do this, you can include the following code in the place where FTPClient used in your webapp:

 FTPClient ftpClient = ...; if(ftpClient.getClass().getClassLoader() instanceof java.net.URLClassLoader) { URL[] urls = ((java.net.URLClassLoader) ftpClient.getClass().getClassLoader()).getURLs(); // log these urls somewhere and check - these are urls from where your FTPClient may be loaded from } 

If FTPClient is not loaded via java.net.URLClassLoader , then it may be more difficult to check the class path, although this should not be a problem.

Hope this helps ...

+3
source

This should be the way you call setConnectTimeout because it exists. setConnectTimeout is not a static call; you must call it after allocating an FTPClient and dialing before connecting.

 FTPClient ftp = new FTPClient(); ftp.setConnectTimeout(5000); // 5000 Milliseconds (5 Seconds) ... ftp.connect(server, port); 
+1
source
  FTPClient ftp = new FTPClient(); ftp.setDefaultTimeout(); ftp.setDataTimeout(); ftp.setConnectTimeout(); ftp.setSoTimeout(); ftp.setControlKeepAliveTimeout(); ftp.setControlKeepAliveReplyTimeout(); 

From the Apache docs:

  /** * Set the default timeout in milliseconds to use when opening a socket. * This value is only used previous to a call to * {@link #connect connect()} * and should not be confused with {@link #setSoTimeout setSoTimeout()} * which operates on an the currently opened socket. _timeout_ contains * the new timeout value. * <p> * @param timeout The timeout in milliseconds to use for the socket * connection. */ void setDefaultTimeout(int timeout); /** * Sets the timeout in milliseconds to use when reading from the * data connection. This timeout will be set immediately after * opening the data connection, provided that the value is &ge; 0. * <p> * <b>Note:</b> the timeout will also be applied when calling accept() * whilst establishing an active local data connection. * @param timeout The default timeout in milliseconds that is used when * opening a data connection socket. The value 0 means an infinite timeout. */ void setDataTimeout(int timeout) /** * Sets the connection timeout in milliseconds, which will be passed to the {@link java.net.Socket} object's * connect() method. * @param connectTimeout The connection timeout to use (in ms) * @since 2.0 */ void setConnectTimeout(int connectTimeout); /** * Set the timeout in milliseconds of a currently open connection. * Only call this method after a connection has been opened * by {@link #connect connect()}. * <p> * To set the initial timeout, use {@link #setDefaultTimeout(int)} instead. * * @param timeout The timeout in milliseconds to use for the currently * open socket connection. * @exception SocketException If the operation fails. * @throws NullPointerException if the socket is not currently open */ void setSoTimeout(int timeout) throws SocketException; /** * Set the time to wait between sending control connection keepalive messages * when processing file upload or download. * * @param controlIdle the wait (in secs) between keepalive messages. Zero (or less) disables. * @since 3.0 * @see #setControlKeepAliveReplyTimeout(int) */ void setControlKeepAliveTimeout(long controlIdle); /** * Set how long to wait for control keep-alive message replies. * * @param timeout number of milliseconds to wait (defaults to 1000) * @since 3.0 * @see #setControlKeepAliveTimeout(long) */ void setControlKeepAliveReplyTimeout(int timeout) 
+1
source

All Articles