1
비동기 프로세스를 실행하고 해당 입력 스트림을 얻으려고합니다 (있는 경우). Futuretask를 사용하는 입력 스트림 리더가있는 Java 비동기 프로세스 러너
이
내 코드입니다 :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;
}
}
실행하는 과정입니다 :
-
을
- 시간 제한을 관리해야하기 때문에 프로세스가 비동기 여야합니다.
- Pro cess의 InputStream은 옵션 일 수 있으며 내용을 기다리는 스레드를 잠그지 않아야합니다. 출력이없는 프로세스가있을 수 있습니다. 나는이 버전을 시도하고있다
UPDATE
... 작동하는 것 같다하지만이 한 정도로 강한 나도 몰라.
@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;
}