SSHClient jsch bufferedinputstream LOCK

时间:2012-10-30 08:26:21

标签: locking jsch

当基于jsch session get lock:

通过SShClient执行命令时
    Exec thread devapp090@1046" prio=5 tid=0x14 nid=NA runnable
    java.lang.Thread.State: RUNNABLE
    at java.io.FileInputStream.readBytes(FileInputStream.java:-1)
    at java.io.FileInputStream.read(FileInputStream.java:220)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
    - locked <0x420> (a java.io.BufferedInputStream)
    at com.jcraft.jsch.ChannelSession.run(ChannelSession.java:245)
    at java.lang.Thread.run(Thread.java:662)

这里我如何称呼客户端:

    client.openSession();
    String[] result = client.execCommands(new String[]{"ps ax|grep karaf"});
    client.openSession();

这里的exec方法:

    public String[] execCommands(String[] commands) throws JSchException, IOException {

    String[] results = new String[commands.length];
    for (int j = 0; j < commands.length; j++) {
        ChannelExec channel = (ChannelExec) session.openChannel("exec");
        channel.setInputStream(System.in, true);
        channel.setCommand(commands[j]);


        channel.connect();
        InputStream in = channel.getInputStream();



        byte[] tmp = new byte[1024];
        StringBuilder result = new StringBuilder();
        long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime < timeout) {
            while (in.available() > 0 && System.currentTimeMillis() - startTime <  timeout) {
                int i = in.read(tmp, 0, 1024);
                if (i < 0) break;
                result.append(new String(tmp, 0, i));
                if (isLoggerEnabled)
                    LOGGER.info(new String(tmp, 0, i));
            }
            if (channel.isClosed()) {
                if (isLoggerEnabled)
                    LOGGER.info("exit-status: " + channel.getExitStatus());
                break;
            }
        }
        in.close();
        channel.disconnect();

        results[j] = result.toString();
    }


    return results;
     }

打开和关闭会话方法:

    public void openSession() throws JSchException {
    JSch jsch = new JSch();
    Session session = jsch.getSession(user, host, port);
    session.setUserInfo(new SSHUserInfo(user, password));
    session.setTimeout(timeout);
    session.connect();
    this.session = session;
    }

    public void closeSession() {
    if (session != null && session.isConnected()) {
        session.disconnect();
    }
    }

我正在关闭输入,断开与频道和会话的连接。但没有任何帮助。我不知道锁定了什么。如果我理解正确,它会锁定从ChannelSession.java中的System.in读取。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

不要这样做channel.setInputStream(System.in,true);