5

尽管我使用 ftpClient.setControlKeepAliveTimeout(CONTROL_CONNECTION_KEEP_ALIVE_INTERVAL_SECONDS); 有时它只是死于 SocketException 而不是正常的 FTPConnectionClosedException。总而言之,FTP 是一种我经常使用的非常狡猾的协议,我连接的每台服务器都需要进行一些调整,但是这个协议非常困难。

我知道我可能做错了一百万件事,我的问题是,是否有一些解决方案已经在 FTP 上实施了重试,以防你失去控制连接,因为这不应该令人震惊(代理/防火墙有时只是随机丢失您的连接)。还是有一些更优雅的方法来解决这个问题。

我有这样的东西。

public void store(File fileToUpload) throws IOException, InterruptedException {
    String filename = fileToUpload.getName();
    int retries = 0;
    while (true) {
        try {
            ftpClient.storeFile(filename, inputStreamFactory.getInputStream(fileToUpload));
        } catch (FTPConnectionClosedException | SocketException exception) {
            LOGGER.debug("Control connection lost uploading {}, continuing.", filename);
        }

        // This sleep is because there's an anti-malware in the servers which makes the file not to appear
        // available immediately after an upload
        LOGGER.debug("Waiting {} milliseconds for anti-malware protection to process file", WAIT_AFTER_UPLOAD_MILLISECONDS);
        threadWrapper.sleep(WAIT_AFTER_UPLOAD_MILLISECONDS);
        if (!ftpClient.isConnected()) {
            connect();
        }

        LOGGER.debug("Checking if {} is already uploaded", filename);
        if (ftpFileChecker.isFileCompleted(listFiles(null), filename, fileToUpload.length())) {
            // Note this is likely to happen every time since their server will close the control
            // connection quite fast and FTPClient uses it at the end of storeFile
            LOGGER.debug("File {} was uploaded correctly", filename);
            break;
        } else {
            if (++retries > MAX_RETRIES) {
                throw new RemoteTimeoutException("Could not upload file, max retries exceeded");
            } else {
                LOGGER.info("File {} was not uploaded, retrying", filename);
            }
        }
    }
}

public void connect() throws IOException, InterruptedException {
    int retries = 0;
    while (true) {
        try {
            ftpClient = ftpClientFactory.createFtpClient();
            ftpClient.connect(server, FTP_PORT);
            if (!ftpClient.login(username, password)) {
                LOGGER.error("Login to FTP failed");
                throw new ConfigurationException("Login to FTP failed");
            }
            ftpClient.enterLocalPassiveMode();
            ftpClient.setControlKeepAliveTimeout(CONTROL_CONNECTION_KEEP_ALIVE_INTERVAL_SECONDS);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.changeWorkingDirectory(uploadDir);
            break;
        } catch (FTPConnectionClosedException | SocketException exception) {
            if (++retries > MAX_RETRIES) {
                throw new RemoteTimeoutException("Could not upload file, max retries exceeded", exception);
            } else {
                LOGGER.info("Could not login, retrying");
            }
        }
        LOGGER.debug("Sleeping {} milliseconds before trying to reconnect", WAIT_BETWEEN_CONNECT_RETRIES_MILLISECONDS);
        threadWrapper.sleep(WAIT_BETWEEN_CONNECT_RETRIES_MILLISECONDS);
    }
}

public FTPFile[] listFiles(String directory) throws IOException, InterruptedException {
    int retries = 0;
    while (true) {
        try {
            return ftpClient.listFiles(directory);
        } catch (FTPConnectionClosedException exception) {
            LOGGER.debug("Control connection lost when listing files, continuing");
        } catch (SocketException exception) {
            LOGGER.debug("Socket exception when listing files, continuing");
        }
        if (!ftpClient.isConnected()) {
            connect();
        }
        if (++retries > MAX_RETRIES) {
            throw new RemoteTimeoutException("Could not list files, max retries exceeded");
        } else {
            LOGGER.info("Could not list files, retrying");
        }
    }
}

现在我在 listFiles 上得到了 SocketException Broken Pipe,我无法弄清楚,因为在我的本地服务器上它运行良好,但在这个特定的服务器上(提示它在 Windows NT 上运行 - :( - 它运行一些恶意软件保护这可以防止文件立即出现在服务器上,并且显然位于一些非常奇怪的防火墙后面,并且它会在大约 5 秒后断开空闲连接,并且他们不会更改配置,因为他们是一家大公司并声称它适用于其他所有人)。

我尝试了 VFS 并调查了其他 FTP 客户端,但我发现没有一个似乎可以解决问题,甚至更没有帮助,它们中的大多数(如 ftp4j)不在 maven Central 中,这真的让我无法尝试它们,除非有保证它会解决我的问题。

欢迎任何帮助。

编辑:给出的代码反映了这个的开始复杂性,我当前的解决方案更稳定,复杂性要高得多,但它一点也不优雅,所以我把这个问题留着,以防有人愿意提供一个好的解决方案。

4

1 回答 1

-1

如果使用 Spring,则考虑 Spring Retry。我相信最新的 Maven 版本是:

    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
        <version>1.1.2.RELEASE</version>
    </dependency>
于 2015-09-30T01:58:42.170 回答