简体   繁体   中英

What is the meaning of different timeout properties of Apache FTPClient (and DefaultFtpSessionFactory in Spring)?

There are following timeout properties used for FTP communication:

  • connectTimeout : connection timeout in milliseconds, which will be passed to the Socket object's connect() method
  • defaultTimeout : default timeout in milliseconds to use when opening a socket
  • dataTimeout : timeout in milliseconds to use when reading from the data connection

Could you explain to Java/Kotlin developer why to set them and what bad things could happen if you do not set them?

To add some context: My server app needs to connect to FTP and list/upload/download/delete files. I would like to be sure my attempts to connect or transfer file won't hang forever in case something goes wrong.

All those properties from the AbstractFtpSessionFactory are propagated down to an FTPClient :

    if (this.connectTimeout != null) {
        client.setConnectTimeout(this.connectTimeout);
    }
    if (this.defaultTimeout != null) {
        client.setDefaultTimeout(this.defaultTimeout);
    }
    if (this.dataTimeout != null) {
        client.setDataTimeout(this.dataTimeout);
    }

The connectTimeout over there has a default value as private static final int DEFAULT_CONNECT_TIMEOUT = 60000; . So, it is OK to miss it. Yes, this one is used when we create a Socket via connect() : _socket_.connect(new .netSocketAddress(host, port), connectTimeout);

The defaultTimeout is propagated down to _socket_.setSoTimeout(_timeout_); which has an effect on socket read operations. See Socket.setSoTimeout() JavaDocs. Yes, its default value is 0 . So, also OK to miss its configuration.

The dataTimeout is used for the server Socket created in a ACTIVE_LOCAL_DATA_CONNECTION_MODE for similar setSoTimeout() option. Default is the same: 0 - infinite wait for read operation answer.

I derived all of that from Apache Commons Net source code.

Doesn't look like the project provides some docs on the matter by itself: https://commons.apache.org/proper/commons.net/

So, yeah, a rule of thumb: always configure those props with a reasonable values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM