[英]Too many connections with apache commons-vfs2
我有一個需要從 sftp 下載文件的應用程序。 我目前正在使用 apache commons-vfs2
我有一個每 1 分鍾運行一次的調度程序。 1. 獲取遠程文件列表(打開連接,獲取列表,然后關閉連接) 2. 從步驟 1 下載文件(打開連接,下載每個文件,然后關閉連接)
如何將連接保持在最低限度? 有沒有辦法限制我與 commons-vfs2 的連接數?
這是我的代碼
private List<FileObject> getRemoteFilesList() throws FileSystemException {
FileObject[] remoteFiles;
try {
manager.init();
final @Cleanup FileObject remoteDirectoryObject = manager.resolveFile(uri, fileSystemOptions);
remoteFiles = remoteDirectoryObject.getChildren();
} finally {
manager.freeUnusedResources();
manager.close();
}
return Arrays.stream(remoteFiles)
.collect(Collectors.toList());
}
private List<File> downloadRemoteFiles(final List<FileObject> remoteFiles) {
if(remoteFiles.isEmpty()) {
return Collections.emptyList();
}
final List<File> myCollection = new ArrayList<>();
try {
manager.init();
for (final FileObject myfile : remoteFiles) {
final File localFile = downloadFile(myfile);
myCollection.add(localFile);
myfile.delete();
}
} catch (final IOException exception) {
log.warn("Unable to download because ", exception);
} finally {
manager.freeUnusedResources();
manager.close();
}
return myCollection;
}
VFS 的 apache 公共 wiki ( https://wiki.apache.org/commons/VfsFaq ) 說在某些情況下關閉 SFTP 連接時使用以下內容:
((DefaultFileSystemManager) fsManager).close();
這會強制調用DefaultFileSystemManager
上的close
方法,而不是FileSystemManager
類上的close
方法。
這可能不是你的問題,但它可能是相關的。
您可以嘗試這種替代方法來清理任何臨時文件並關閉所有提供程序。
FileObject src = null;
/**
* Release system resources, close connection to the filesystem.
*/
public void release() {
FileSystem fs = null;
this.src.close(); // Seems to still work even if this line is omitted
fs = this.src.getFileSystem(); // This works even after the src is closed.
this.fsManager.closeFileSystem(fs);
}
可以在以下位置找到更多詳細信息: https : //cwiki.apache.org/confluence/display/COMMONS/SimpleSftpFileDownload
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.