Philipp Hoffmann
Philipp Hoffmann

Reputation: 93

Commons FTPClient - storeFile() hangs, if FTP server becomes unavailable

I use Apache's FTPClient to upload files to a FTP server.

However, even the FTP servers becomes unavailable while we upload a file, storeFile() hangs. storeFile() does not cancel the upload.

tcpdump trace:

22:04:29.584767 IP 10.84.78.2 > 192.168.114.2: ICMP 10.84.78.2 tcp port 53751 unreachable, length 142

Is there a way to set timeout? I already tried to use setSoTimeout() after connect() and setDataTimeout() before storeFile(). But this attributes seems to be irrelevant for this issue.

Thread dump, taken after FTP server is unavailable:

    "Thread-1" prio=10 tid=0x00007f1a700f1800 nid=0x479b runnable [0x00007f1a76fea000]
       java.lang.Thread.State: RUNNABLE
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.write(Unknown Source)
- locked <0x00000000bb026df8> (a java.io.BufferedOutputStream)
at org.apache.commons.net.io.ToNetASCIIOutputStream.write(ToNetASCIIOutputStream.java:75)
- locked <0x00000000bb028e20> (a org.apache.commons.net.io.ToNetASCIIOutputStream)
at org.apache.commons.net.io.Util.copyStream(Util.java:111)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:653)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:624)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1976)
at ... (FTPClient.java:103)
at ... (Sender.java:67)

netstat -anpo output:

netstat -anpo | grep 192

tcp6       0      0 10.84.78.2:9011         192.168.114.2:21        VERBUNDEN   19310/java       aus (0.00/0/0)
tcp6       0 201480 10.84.78.2:33088        192.168.114.2:20        VERBUNDEN   19310/java       ein (10,26/2/0)

Upvotes: 3

Views: 3113

Answers (1)

2Big2BeSmall
2Big2BeSmall

Reputation: 1378

I would try it with

 ftpClient.setControlKeepAliveTimeout(300); // set timeout to 5 minutes

Taken from here

"During file transfers, the data connection is busy, but the control connection is idle. FTP servers know that the control connection is in use, so won't close it through lack of activity, but it's a lot harder for network routers to know that the control and data connections are associated with each other. Some routers may treat the control connection as idle, and disconnect it if the transfer over the data connection takes longer than the allowable idle time for the router. One solution to this is to send a safe command (i.e. NOOP) over the control connection to reset the router's idle timer. This is enabled as follows:"

 ftpClient.setControlKeepAliveTimeout(300); // set timeout to 5 minutes

This will cause the file upload/download methods to send a NOOP approximately every 5 minutes.

The following public methods support this:"
retrieveFile(String, OutputStream)
appendFile(String, InputStream)
storeFile(String, InputStream)
storeUniqueFile(InputStream)
storeUniqueFileStream(String)

Upvotes: 1

Related Questions