【问题标题】:Getting the list of files in different directory parallel from SFTP server using JSch使用 JSch 从 SFTP 服务器并行获取不同目录中的文件列表
【发布时间】:2017-05-20 10:34:03
【问题描述】:

我正在使用带有线程组(不同会话)的 JSch 从 SFTP 服务器并行获取不同目录中的文件列表,我正面临:

"FileProcessThread4" prio=10 tid=0x00007fd826c9e000 nid=0x1e0c waiting for monitor entry [0x00007fd841824000]
   java.lang.Thread.State: BLOCKED (on object monitor)

问题是因为 JSch 0.1.54 还是并行?

使用 find 命令获取最近 10 分钟的旧文件,

我正在使用的代码:

public static String[] getRemoteFiles(String host, String user, String password, String command, String port) {
    String line = null;
    Session session = null;
    ChannelExec channelExec = null;
    InputStream in = null;
    List<String> list = null;
    BufferedReader reader = null;
    try {
        list = new ArrayList<String>();
        JSchUtil jSchUtil = new JSchUtil();
        session = jSchUtil.connect(host, user, password, port);
        channelExec = (ChannelExec) session.openChannel("exec");
        logger.debug("SSH exec channel opened");
        in = channelExec.getInputStream();
        channelExec.setCommand(command);
        channelExec.connect()
        reader = new BufferedReader(new InputStreamReader(in));

        //Here block is happens
        while ((line = reader.readLine()) != null) {
            list.add(line);
        }

    } catch (Exception exception) {
        logger.error("Exception-->" + exception.getMessage(), exception);
    } finally {
        if (channelExec != null) {
            try {
                channelExec.disconnect();
            } catch (Exception exception) {
                logger.error("Exception-->" + exception.getMessage(), exception);
            } finally {
                channelExec = null;
            }
        }
        if (session != null) {
            try {
                session.disconnect();
            } catch (Exception exception) {
                logger.error("Exception-->" + exception.getMessage(), exception);
            } finally {
                session = null;
            }
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception exception) {
                logger.error("Exception-->" + exception.getMessage(), exception);
            } finally {
                reader = null;
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (Exception exception) {
                logger.error("Exception-->" + exception.getMessage(), exception);
            } finally {
                in = null;
            }
        }
    }
    return list.toArray(new String[] {});
}

public class JSchUtil {

private Logger logger = Logger.getLogger(JSchUtil.class);

private static final String STRICT_HOSTKEY_CHECKIN_KEY = "StrictHostKeyChecking";
private static final String STRICT_HOSTKEY_CHECKIN_VALUE = "no";

public Session connect(String host, String username, String password, String port) throws Exception {
    JSch jsch = new JSch();
    Session sshSession = jsch.getSession(username, host, Integer.valueOf(port));
    sshSession.setUserInfo(new UserInfo() {
        public String getPassphrase() {
            return null;
        }

        public String getPassword() {
            return null;
        }

        public boolean promptPassphrase(String string) {
            return false;
        }

        public boolean promptPassword(String string) {
            return false;
        }

        public boolean promptYesNo(String string) {
            return true;
        }

        public void showMessage(String string) {
        }
    });
    sshSession.setPassword(password);
    Properties config = new Properties();
    config.put(STRICT_HOSTKEY_CHECKIN_KEY, STRICT_HOSTKEY_CHECKIN_VALUE);
    sshSession.setConfig(config);
    logger.debug("SSH_Connection|Host->" + host + "|Port->" + port + "|Username->" + username + "|Password->" + password);
    sshSession.connect();
    logger.debug("SSH_Connection|Status->Success");
    return sshSession;
}

}

感谢您的建议

【问题讨论】:

  • 向我们展示您的代码!
  • 问题中添加的代码。
  • 代码与SFTP无关
  • 正确,没有 SFTP 通道,但是当我获取带有 exec 通道的列表时,线程在代码中的注释行被阻塞。
  • 那么为什么你的问题是“SFTP”?为什么你甚至使用“exec”通道检索文件列表?为什么不使用 SFTP?

标签: java multithreading sftp jsch


【解决方案1】:

我之前使用过这个库,执行各种语句的顺序如下(这不会编译;只是显示要调用的各种方法及其顺序):

session.openChannel("exec");
channel.setCommand(...);
reader = new BufferedReader(new InputStreamReader(channel.getInputStream()));
channel.connect();

// Wait for command to terminate
while ((exit_code = channel.getExitStatus()) == -1)
{
  // Sleep for, for instance, 100ms
}

// Now read the output
while (reader.ready() && ((line = reader.readLine()) != null))
{
  ...
}

channel.disconnect();

【讨论】:

  • 我们可以从 JSch SFTP 通道并行下载具有不同会话的多个文件,对吗?
  • 我没有理由不这样做
  • 我更新了我的答案。检查语句的顺序。
  • 遇到了同样的问题,可能已经找到了解决方案。查看编辑后的答案
猜你喜欢
  • 2020-01-28
  • 1970-01-01
  • 2017-12-14
  • 1970-01-01
  • 2022-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-10
相关资源
最近更新 更多