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.
CodePudding user response:
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 InetSocketAddress(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.