使用JSch从SFTP服务器获取不同目录中的文件列表

时间:2017-05-20 10:34:03

标签: java multithreading sftp jsch

我使用带有线程组的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分钟的旧文件,

代码iam使用:

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;
}

}

感谢您的建议

1 个答案:

答案 0 :(得分:0)

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

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();