SSHClient jsch bufferedinputstream LOCK

旧街凉风 提交于 2019-12-08 09:10:03

问题


When executing command via SShClient based on jsch session get lock:

    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)

Here how I call client:

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

Here exec method:

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

Open and close session methods:

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

I'm closing input, disconnecting from channel and session. Butnothing helps. And I don't know what is locked. If I'm understand right it is locking on reading from System.in in ChannelSession.java. Have any ideas?


回答1:


don't do this channel.setInputStream(System.in, true);



来源:https://stackoverflow.com/questions/13135165/sshclient-jsch-bufferedinputstream-lock

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!