Browse Source

任务详情导出,ftp连接优化

master
wangguangyuan 3 months ago
parent
commit
43df92ab68
1 changed files with 42 additions and 21 deletions
  1. +42
    -21
      inspect-base/inspect-base-core/src/main/java/com/inspect/base/core/sftp/SftpFactory.java

+ 42
- 21
inspect-base/inspect-base-core/src/main/java/com/inspect/base/core/sftp/SftpFactory.java View File

@ -68,31 +68,52 @@ public class SftpFactory {
} }
public FTPSClient connect() throws IOException { public FTPSClient connect() throws IOException {
boolean isImplicit = "true".equals(sftpLoginConfig.getIsImplicit());
//boolean isImplicit = Boolean.getBoolean(sftpLoginConfig.getIsImplicit());
logger.info(Color.MAGENTA + "[FTP] isImplicit: {}" + Color.END, isImplicit);
FTPSClient ftps = new FTPSClient(isImplicit);
try {
ftps.setControlEncoding("UTF-8");
ftps.connect(sftpLoginConfig.getIp(), Integer.parseInt(sftpLoginConfig.getPort()));
} catch (Exception e) {
logger.error("[FTP] CONN FAIL: {}, CAUSE: {}", JSONObject.toJSONString(sftpLoginConfig), e.getMessage());
int maxRetries = 5;
int retryCount = 0;
long retryDelay = 1000L;
while (retryCount < maxRetries) {
boolean isImplicit = "true".equals(sftpLoginConfig.getIsImplicit());
//boolean isImplicit = Boolean.getBoolean(sftpLoginConfig.getIsImplicit());
logger.info(Color.MAGENTA + "[FTP] isImplicit: {}" + Color.END, isImplicit);
FTPSClient ftps = new FTPSClient(isImplicit);
try { try {
Thread.sleep(1000L);
} catch (InterruptedException e2) {
ftps.setControlEncoding("UTF-8");
ftps.connect(sftpLoginConfig.getIp(), Integer.parseInt(sftpLoginConfig.getPort()));
boolean login = ftps.login(sftpLoginConfig.getUsername(), sftpLoginConfig.getPassword());
if (!login) {
throw new ServiceException("LOGIN TO FTP FAIL!");
} else {
return ftps;
}
} catch (Exception e) {
retryCount++;
logger.error("[FTP] CONN FAIL: {}, (attempt {}/{}): CAUSE: {}",
JSONObject.toJSONString(sftpLoginConfig), retryCount, maxRetries, e.getMessage());
// 清理资源
if (ftps != null && ftps.isConnected()) {
try {
ftps.disconnect();
} catch (IOException ex) {
logger.debug("[FTP] Error during disconnect", ex);
}
}
;
if (retryCount < maxRetries) {
try {
logger.info("[FTP] Retrying in {} ms", retryDelay);
Thread.sleep(retryDelay);
retryDelay *= 2;
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new IOException("Interrupted during retry", ie);
}
} else {
throw new IOException("Failed to connect after " + maxRetries + " attempts", e);
}
} }
return connect();
}
boolean login = ftps.login(sftpLoginConfig.getUsername(), sftpLoginConfig.getPassword());
if (!login) {
throw new ServiceException("LOGIN TO FTP FAIL!");
} else {
return ftps;
} }
throw new IOException("Unexpected error in connection process");
} }
public static void main(String[] args) { public static void main(String[] args) {


Loading…
Cancel
Save