2017-07-18 81 views
0

我試圖通過SSH與JSCH庫連接到另一臺服務器,在那裏執行一些命令(使用TZ檢查當前日期)並返回結果。通過SSH使用JSCH連接時「TIMED_WAITING」線程狀態

代碼如下所示:

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

誰能請幫助呢?

+0

如果你有一些方法或塊進行同步請分享 –

+0

什麼是'command'?它有什麼作用? –

+0

Martin,命令很簡單 - 「date +'%:z'」 –

回答

0

您的代碼完美工作我剛剛在主要方法中運行它。正如我可以看到你在weblogic下運行它,所以它可能是一個原因。

我不知道確切,但我建議你試試這個:

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