非同期プロセスを実行して、その入力ストリームを取得しようとしています (存在する場合)。
これは私のコードです:
CommandCall commandCall = new CommandCall(commands);
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Integer> task = executor.submit(commandCall);
これはプロセス実行タスクです
public class CommandCall implements Callable<Integer> {
private byte[] output;
private int retval=-1;
private String[] commands=null;
Process process=null;
public CommandCall(String[] commands) throws Exception {
this.commands=commands;
this.output=null;
}
public void destroy() {
try {
if(process!=null) process.destroy();
}catch(Exception e){}
}
public byte[] getByteArray() {
return output;
}
public int getRetval() {
return retval;
}
@Override
public Integer call() throws Exception {
try{
process = new ProcessBuilder(commands).start();
// here i must read the process input and store it to this.output
// this must be a non lockable read because the process can be without any output
retval= process.waitFor();
}finally{
try{
if(bos!=null) bos.close();
}catch(Exception e){}
}
return retval;
}
}
プロセスの出力を取得できません。次の 2 つの非常に重要な点に注意してください。
- タイムアウトを管理する必要があるため、プロセスは非同期でなければなりません
- プロセスの InputStream はオプションにすることができ、コンテンツを待機しているスレッドをロックしてはなりません。出力のないプロセスが存在する可能性があります。
アップデート
私はこのバージョンを試しています...うまくいくようですが、十分に強いかどうかはわかりません。
@Override
public Integer call() throws Exception {
InputStream is=null;
try{
process = new ProcessBuilder(commands).start();
is=process.getInputStream();
int len;
int size = 1024;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[size];
while ((len = is.read(buf, 0, size)) != -1)
bos.write(buf, 0, len);
output = bos.toByteArray();
retval= process.waitFor();
}finally{
try{
if(is!=null) is.close();
}catch(Exception e){}
}
return retval;
}