[英]Too many connections with apache commons-vfs2
I have an application that needs to download files from sftp.我有一个需要从 sftp 下载文件的应用程序。 I'm currently using apache commons-vfs2
我目前正在使用 apache commons-vfs2
I have a scheduler that runs every 1 minute.我有一个每 1 分钟运行一次的调度程序。 1. Get the list of files that's on remote (open connection, get the list, then close connection) 2. Download the files from step 1 (open connection, download each files, then close connection)
1. 获取远程文件列表(打开连接,获取列表,然后关闭连接) 2. 从步骤 1 下载文件(打开连接,下载每个文件,然后关闭连接)
How can I keep the connections to minimum?如何将连接保持在最低限度? Is there a way to limit how many connections I have with commons-vfs2?
有没有办法限制我与 commons-vfs2 的连接数?
Here is my code这是我的代码
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;
}
The apache commons wiki for VFS ( https://wiki.apache.org/commons/VfsFaq ) says to use the following when closing an SFTP connection in certain circumstances: VFS 的 apache 公共 wiki ( https://wiki.apache.org/commons/VfsFaq ) 说在某些情况下关闭 SFTP 连接时使用以下内容:
((DefaultFileSystemManager) fsManager).close();
This forces the close
method on the DefaultFileSystemManager
to be called, rather than the close
method on the FileSystemManager
class.这会强制调用
DefaultFileSystemManager
上的close
方法,而不是FileSystemManager
类上的close
方法。
It may not be your issue, but it's something that may be related.这可能不是你的问题,但它可能是相关的。
You can try this alternative method to clean up any temporary files and close all providers.您可以尝试这种替代方法来清理任何临时文件并关闭所有提供程序。
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);
}
More details can be found on: https://cwiki.apache.org/confluence/display/COMMONS/SimpleSftpFileDownload可以在以下位置找到更多详细信息: https : //cwiki.apache.org/confluence/display/COMMONS/SimpleSftpFileDownload
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.