I have a program with 2 process java : processA and processB ( 2 process java.exe not 2 threads).
i use code block below from process A to invoke processB this code is wrapped in RunTask class below
public class RunTask implements Callable<Object> {
private String runParams;
public String getRunParams() {
return runParams;
}
public void setRunParams(String runParams) {
this.runParams = runParams;
}
@Override
public Object call() throws Exception {
try {
//System.out.println("run:" + runParams);
Process procB = Runtime.getRuntime().exec("java -jar processB.jar);
DataInputStream ls_in = new DataInputStream(procB.getInputStream());
String ls_str;
while ((ls_str = ls_in.readLine()) != null) {
System.out.println(ls_str);
}
} catch (Exception exp) {
exp.printStackTrace();
}
return null;
}
}
and Main class i use executor
ExecutorService eservice = Executors.newSingleThreadExecutor();
while (1 == 1) {
String stringParams = getFilesNeedToImportAsString();
if (stringParams.trim().isEmpty()) {
long l1 = System.currentTimeMillis() - l;
System.out.println("all time" + l1 / 1000);
System.exit(100);
}
RunTask runTask = new RunTask();
runTask.setRunParams(SystemInfo.RUN_COMMAND + stringParams);
Future<Object> objectFuture = eservice.submit(runTask);
while (!objectFuture.isDone()) {
System.out.println("waiting the task running");
Thread.sleep(500);
}
}
But when an exception occurred on processB both processes (processA,processB) seem be halted this is code run on processB
public Object call() {
try {
MutationResult result = mutator.execute();
return "ok";
} catch (Exception exp) {
exp.printStackTrace();
System.out.println("error on " + Thread.currentThread().getName() + "failed begin retry " + (++retryCount));
call();
System.out.println(retryCount + " completed");
return "ok";
}
}
if i run processB stand alone (by command line) it’s never happen, or when this problem occured i use taskmanager to kill proceesA (callee), processB continue to run
Please any one give me the solution for this problem !!
You wait on
objectFutureisDonemethod to complete. According to the documentation:Looking at the code in processB when an exception occurs you execute
call()again recursively. I understand that you do this as a error retry mechanism but this is a really bad idea for two main reasons:– If the exception persists you will end up getting a
StackOverflowException.– None of the conditions for
isDonewill be met.A better alternative will be defining a maximum number of retries and try executing
mutator.execute(). If the error persists then throw and exception and finish execution.Another option would be to wait to processB to finish for a maximum time after which you cancel the execution of the task by calling
objectFuture.cancel(true).A couple of comments on processA: On
RunTaskyou don’t userunParamswhich seem to be some files that need to be imported as string. Also, you usereadLine()onDataInputStreamwhich is a deprecated method. Instead use anInputStream(e.g.BufferedInputStream) wrapped in anInputStreamReaderand then wrapped in aBufferedReader– then callreadLine()on theBufferedReader.