2013-08-01 65 views
1

直接粘貼的代碼示例的InputStream閱讀:使用的Runtime.exec()啓動WebSphere的wsadmin.bat - 掛起,而從工藝

 Runtime rt = Runtime.getRuntime(); 
     path = "c:\IBM\WebSphere\AppServer\profiles\STSCDmgrProfile\bin\"; 
     cmd = path + "wsadmin"; 
     String cmdString = cmd 
       + " -host " 
       + host 
       + " -port " 
       + port 
       + " -username " 
       + username 
       + " -password " 
       + password 
       + " " 
       + "-f" + "c:/IBM/WebSphere/AppServer/profiles/STSCDMgrProfile/temp/mergedScripts.jy" 
       + " -lang " 
       + lang 
       + " -tracefile logs/ssc_wsadmin_trace.txt -appendtrace true"; 
     _logger.finer(cmdString.replaceAll(" " + password, " <password>")); 
     Process proc = rt.exec(cmdString); 
     _logger.finer("Launched process"); 

     stdInput = new BufferedReader(new InputStreamReader(proc 
       .getInputStream())); 
     stdError = new BufferedReader(new InputStreamReader(proc 
       .getErrorStream())); 

     // read the output from the command 
     String sIn = ""; 
     **while ((sIn = stdInput.readLine()) != null) {** 
      _logger.log(Level.FINE, "runJCommand stin ==>", sIn); 
     } 

但是它掛起,而這樣做從stdInput的readLine()。以上突出顯示。以下是我在跟蹤日誌中看到:

[7/30/13 23:48:04:937 GMT-12:00] 000000a6 ThreadMonitor W WSVR0605W: Thread "WebContainer : 1" (00000175) has been active for 664085 milliseconds and may be hung. There is/are 1 thread(s) in total in the server that may be hung. 
at java.io.FileInputStream.readBytes(Native Method) 
at java.io.FileInputStream.read(FileInputStream.java:223) 
at java.io.BufferedInputStream.read1(BufferedInputStream.java:267) 
at java.io.BufferedInputStream.read(BufferedInputStream.java:328) 
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:464) 
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:506) 
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:234) 
at java.io.InputStreamReader.read(InputStreamReader.java:188) 
at java.io.BufferedReader.fill(BufferedReader.java:147) 
at java.io.BufferedReader.readLine(BufferedReader.java:310) 
**at java.io.BufferedReader.readLine(BufferedReader.java:373)** 
at com.ibm.sametime.console.admin.plugins.wsadmin.SSCWsAdmin.runJCommand(SSCWsAdmin.java:924) 

如果我運行相同的mergedScripts.jy(即我在上面的調用Java代碼中),手動通過命令行,然後將它成功執行,並在幾分鐘完成。然而,通過Java代碼它永遠運行。

可能的原因是什麼?在上述情況下STDIN究竟是什麼?

回答

0

在您從InputStream中讀取的示例代碼中,但根本沒有從ErrorStream中讀取,您的問題可能是ErrorStream的緩衝區已滿,導致進程阻塞或死鎖。請參閱「爲什麼Runtime.exec()掛起」部分in this article。本文繼續介紹如何避免這種情況,但您可以使用ProcessBuilder而不是Runtime.exec()來簡化過程,然後重定向ErrorStream。

ProcessBuilder pb = new ProcessBuilder("wsadmin", "-host", host, "-port", port, "-username", username, ...); 
Process proc = pb.redirectErrorStream(true).start(); 
//Read from the InputStread as you were... 

其實什麼是STDIN在上述情況下?

proc.getInputStream()將包含運行命令wsadmin時從命令行運行時產生的任何輸出。

0

我不確定你到底在幹什麼,但是我曾經寫過類似於你在Perl中做的事情。也許你可以適應python。

my $line = "cd /usr/websphere/profiles/myProfile/bin;"; 
$line .= ($user !~ /root/i) ? ' sesudo' : ''; 
$line .= " ./wsadmin.sh -lang jython "; 
$line .= "-f $wasscriptpth ".join(' ', @args); 
$line .= " 2>&1"; 

然後,我只是呼籲shell的命令和管道輸出到一個文件句柄...

open(SSHANDLE, "$line |") 

注「2> & 1」這是爲了讀一個非常簡單的方法在一個地方的錯誤和標準輸出。也許你可以在Python中做這樣的事情?