Apache Commons FTPClient挂起

IAm*_*aja 20 java ftp ftp-client apache-commons-net

我们使用以下Apache Commons Net FTP代码连接到FTP服务器,轮询某些目录以查找文件,如果找到文件,则将它们检索到本地计算机:

try {
logger.trace("Attempting to connect to server...");

// Connect to server
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(20000);
ftpClient.connect("my-server-host-name");
ftpClient.login("myUser", "myPswd");
ftpClient.changeWorkingDirectory("/loadables/");

// Check for failed connection
if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
{
    ftpClient.disconnect();
    throw new FTPConnectionClosedException("Unable to connect to FTP server.");
}

// Log success msg
logger.trace("...connection was successful.");

// Change to the loadables/ directory where we poll for files
ftpClient.changeWorkingDirectory("/loadables/");    

// Indicate we're about to poll
logger.trace("About to check loadables/ for files...");

// Poll for files.
FTPFile[] filesList = oFTP.listFiles();
for(FTPFile tmpFile : filesList)
{
    if(tmpFile.isDirectory())
        continue;

    FileOutputStream fileOut = new FileOutputStream(new File("tmp"));
    ftpClient.retrieveFile(tmpFile.getName(), fileOut);
    // ... Doing a bunch of things with output stream
    // to copy the contents of the file down to the local
    // machine. Ommitted for brevity but I assure you this
    // works (except when the WAR decides to hang).
    //
    // This was used because FTPClient doesn't appear to GET
    // whole copies of the files, only FTPFiles which seem like
    // file metadata...
}

// Indicate file fetch completed.
logger.trace("File fetch completed.");

// Disconnect and finish.
if(ftpClient.isConnected())
    ftpClient.disconnect();

logger.trace("Poll completed.");
} catch(Throwable t) {
    logger.trace("Error: " + t.getMessage());
}
Run Code Online (Sandbox Code Playgroud)

我们计划每分钟运行一次.当部署到Tomcat(7.0.19)时,此代码加载完全正常并开始正常工作.每次,在某些时候,它似乎只是挂起.我的意思是:

  • 不存在堆转储
  • Tomcat仍在运行(我可以看到它的pid并可以登录到web管理器应用程序)
  • 在经理应用程序内部,我可以看到我的WAR仍在运行/启动
  • catalina.out 而我的应用程序特定的日志显示没有任何异常被抛出的迹象

所以JVM仍然在运行.Tomcat仍在运行,我部署的WAR仍在运行,但它只是挂起.有时它运行2个小时然后挂起; 其他时间它运行了几天然后挂起.但是当它挂起时,它会在读取的行About to check loadables/ for files...(我在日志中看到的)和读取的行File fetch completed.(我看不到)之间执行.

这告诉我在文件的实际轮询/获取期间发生了挂起,这种指向与我能够找到哪个与FTPClient死锁有关的问题的方向相同.这让我想知道这些是否是同样的问题(如果是的话,我会高兴地删除这个问题!).但是我不认为相信它们是相同的(我在日志中没有看到相同的例外).

一位同事提到它可能是一个"被动"与"主动"FTP的事情.没有真正知道的区别,我由FTPClient场有点困惑ACTIVE_REMOTE_DATA_CONNECTION_MODE,PASSIVE_REMOTE_DATA_CONNECTION_MODE等等,不知道为什么情绪这么想过,作为一个潜在的问题.

由于我Throwable作为最后的手段在这里捕捉s,如果出现问题,我本来希望在日志中看到一些东西.因此,我觉得这是一个明确的问题.

有任何想法吗?不幸的是,我对这里的FTP内部知识不够了解,无法做出明确的诊断.这可能是服务器端的东西吗?与FTP服务器有关?

tjg*_*184 29

这可能是一些事情,但你朋友的建议是值得的.

试着ftpClient.enterLocalPassiveMode();看看它是否有帮助.

我还建议将断开连接在finally块中,以便它永远不会离开那里.


mol*_*vec 20

昨天,我没睡觉,但我想我解决了这个问题.

您可以使用FTPClient.setBufferSize()增加缓冲区大小;

   /**
 * Download encrypted and configuration files.
 * 
 * @throws SocketException
 * @throws IOException
 */
public void downloadDataFiles(String destDir) throws SocketException,
        IOException {

    String filename;
    this.ftpClient.connect(ftpServer);
    this.ftpClient.login(ftpUser, ftpPass);

    /* CHECK NEXT 4 Methods (included the commented) 
    *  they were very useful for me!
    *  and icreases the buffer apparently solve the problem!!
    */
    //  ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
    log.debug("Buffer Size:" + ftpClient.getBufferSize());
    this.ftpClient.setBufferSize(1024 * 1024);
    log.debug("Buffer Size:" + ftpClient.getBufferSize());


    /*  
     *  get Files to download
     */
    this.ftpClient.enterLocalPassiveMode();
    this.ftpClient.setAutodetectUTF8(true);
            //this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    this.ftpClient.enterLocalPassiveMode();
    FTPFile[] ftpFiles = ftpClient
            .listFiles(DefaultValuesGenerator.LINPAC_ENC_DIRPATH);

    /*
     * Download files
     */
    for (FTPFile ftpFile : ftpFiles) {

        // Check if FTPFile is a regular file           
        if (ftpFile.getType() == FTPFile.FILE_TYPE) {
            try{

            filename = ftpFile.getName();

            // Download file from FTP server and save
            fos = new FileOutputStream(destDir + filename);

            //I don't know what useful are these methods in this step
            // I just put it for try
            this.ftpClient.enterLocalPassiveMode();
            this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            this.ftpClient.setAutodetectUTF8(true);
            this.ftpClient.enterLocalPassiveMode();

            ftpClient.retrieveFile(
                    DefaultValuesGenerator.LINPAC_ENC_DIRPATH + filename,
                    fos
                    );

            }finally{
                fos.flush();
                fos.close();                }
        }
    }
    if (fos != null) {
        fos.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这段代码对某人有用!

  • 你昨天为什么不睡觉? (7认同)
  • 这对我非常有用,谢谢!重要的是this.ftpClient.setBufferSize(1024*1024); (4认同)