java - 与 apache commons-vfs2 的连接过多

标签 java apache sftp apache-commons apache-commons-vfs

我有一个应用程序需要从 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 commons wiki (https://wiki.apache.org/commons/VfsFaq) 说在某些情况下关闭 SFTP 连接时使用以下内容:

((DefaultFileSystemManager) fsManager).close();

这会强制调用 DefaultFileSystemManager 上的 close 方法,而不是 FileSystemManager 上的 close 方法> 类。

这可能不是您的问题,但可能是相关的。

关于java - 与 apache commons-vfs2 的连接过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53180385/

相关文章:

java - 不使用synchronized的引用类型的线程安全

java - 如何让spring mvc Controller 更新多个表?

java - 如何在 Android 上升级 Apache HttpClient 版本

mysql - DBDParams 密码转义

python - Apache Web 服务器上的 Django 'dict' 对象没有属性 'render_context'

python - Paramiko身份验证失败/身份验证异常

java - Spring 启动错误: Failed to refresh live data from process 90196

java - 从 GitHub 加载 Maven 依赖项

java - JSch ChannelSftp.ls - 在 java 中传递匹配模式

python - 如何使用 django-storage sftp 下载文件?