1

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

这里执行方法:

    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 读取。有什么想法吗?

4

1 回答 1

0

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

于 2012-10-31T05:16:27.040 回答