Java – Detect or prevent Commons Net FTPClient from blocking on read

apache-commons-netftpftp-clientjava

I'm using the Commons Net FTPClient class to periodically poll an ftp site and attempt to download a file. Occasionally the thread is blocking indefinitely when trying to read from the site and I wondered if anyone else had encountered this problem and could offer any workarounds?

I'm using FTPClient's retrieveFileStream(String) method to get a handle to an InputStream, which I then attempt to read from. Below is a partial stack trace of the blocked thread.

"FTP File Poll Thread" prio=10 tid=0x083b0800 nid=0x6324 runnable [0x65b5b000..0x65b5beb0]
   java.lang.Thread.State: RUNNABLE
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:129)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
        - locked <0x5c851928> (a java.io.BufferedInputStream)
        at java.io.FilterInputStream.read(FilterInputStream.java:66)
        at java.io.PushbackInputStream.read(PushbackInputStream.java:122)
        at org.apache.commons.net.io.FromNetASCIIInputStream.__read(FromNetASCIIInputStream.java:75)
        at org.apache.commons.net.io.FromNetASCIIInputStream.read(FromNetASCIIInputStream.java:170)
        at java.io.FilterInputStream.read(FilterInputStream.java:116)

Best Answer

java.io is blocking, you need to use the java.nio package. Never used ftp with java.nio, but assume it must work because http can work with java.nio.

Regards

Related Topic