本文介绍了SSHClient jsch bufferedinputstream LOCK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当基于jsch会话通过SShClient执行命令时,获得锁:

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

这里的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读取.有什么想法吗?

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?

推荐答案

不要执行此channel.setInputStream(System.in,true);

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

这篇关于SSHClient jsch bufferedinputstream LOCK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 05:22