使用JSCH通过SSH连接时的“TIMED_WAITING”线程状态

时间:2017-07-18 14:48:45

标签: java multithreading ssh jsch thread-dump

我正在尝试通过SSH与JSCH库连接到另一台服务器,在那里执行一些命令(用TZ检查当前日期)并返回结果。

代码如下所示:

private String processSSHCommand(String host, int port, String user, String password, String command) throws JSchException, IOException {
                Session session = new JSch().getSession(user, host, port);
                session.setPassword(password);
                Properties config = new Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);
                session.connect();
                ChannelExec channel = ((ChannelExec) session.openChannel("exec"));
                channel.setCommand(command);
                channel.connect();
                String s = IOUtils.toString(channel.getInputStream(), "UTF-8");
                channel.disconnect();
                session.disconnect();
                return s;
            }

当此代码被卡住时,我的线程转储:

"[STUCK] ExecuteThread: '12' for queue: 'weblogic.kernel.Default (self-tuning)'" #35 daemon prio=1 os_prio=0 tid=0x00007fb3d4509000 nid=0xc87 in Object.wait() [0x00007fb3ec9d1000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
 at java.lang.Object.wait(Native Method)
 at java.io.PipedInputStream.read(PipedInputStream.java:326)
 - locked <0x00000007bcad22a0> (a com.jcraft.jsch.Channel$MyPipedInputStream)
 at java.io.PipedInputStream.read(PipedInputStream.java:377)
 - locked <0x00000007bcad22a0> (a com.jcraft.jsch.Channel$MyPipedInputStream)
 at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
 at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
 at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
 - locked <0x00000007bcad5168> (a java.io.InputStreamReader)
 at java.io.InputStreamReader.read(InputStreamReader.java:184)
 at java.io.Reader.read(Reader.java:140)
 at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1128)
 at org.apache.commons.io.IOUtils.copy(IOUtils.java:1104)
 at org.apache.commons.io.IOUtils.copy(IOUtils.java:1078)
 at org.apache.commons.io.IOUtils.toString(IOUtils.java:382)
 ...processSSHCommand(...)

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

你的代码工作得很完美我只是在main方法中运行它。我可以看到你在weblogic下运行它所以它可能是一个原因。

我不确定,但我建议你试试这个:

ChannelExec channel = ((ChannelExec) session.openChannel("exec"));
channel.setInputStream(null); // set to null
channel.setCommand(command);
channel.connect();