Dany Lavrov Dany Lavrov - 1 month ago 13
Java Question

communication between a parent process and a child process

I have a class that acts as a parent process. During its run, it creates many child process and runs them simultaneously. Each child process is basically an HTTP client that connects to a service and pulls data from it.

Currently, if one of the child processes stop working from any reason, the parent process re-establishing the connection by restarting that same child process.

The disconnection of the child process may be caused by several thing. I would like to communicate the reason for disconnection from the child process to the parent process and have the parent process act accordingly based on the reason of disconnection (socket read fail, 404 not found, 401 unauthorized etc.).

Is it possible? What would be the shortest/best way to do it?
Here is my Parent class:

public class Parent {
public static void main(String[] args){
List<Process> PRlist = new ArrayList<Process>();
List<String[]> commandsList = new ArrayList<String[]>();
DateTimeFormatter frmt = DateTimeFormatter.ofPattern("hh:mm:ss");

if (args.length == 2 && args[0].matches("-f")){
String dir = System.getProperty("user.dir");
String path = dir + "/" + args[1];
try {
FileReader fr = new FileReader(path);
BufferedReader bf = new BufferedReader(fr);

String line = "";
while ((line = bf.readLine()) != null){
String[] tk = line.split(" ");
String[] cmd = {"java", "-jar", "Child.jar", "-a", tk[0], "-p", tk[1],
"-u", tk[2], "-pw", tk[3], "-m", tk[4], "-s", tk[5]};
Process pr = new ProcessBuilder().inheritIO().command(cmd).redirectInput(Redirect.INHERIT).start();
PRlist.add(pr); commandsList.add(cmd);
}
}
catch (FileNotFoundException ex) {ex.printStackTrace();}
catch (IOException ex) {ex.printStackTrace();}

int streamnum = PRlist.size();

while (true){
for (int i = 0; i < streamnum; i++){
if (!PRlist.get(i).isAlive()){
PRlist.get(i).destroy();
PRlist.remove(i);
try {
Process PR = new ProcessBuilder().inheritIO().command(commandsList.get(i)).redirectInput(Redirect.INHERIT).start();
System.out.println(commandsList.get(i)[12] + " stream re-established at " + LocalDateTime.now().format(frmt));
PRlist.add(i,PR);
} catch (IOException ex) {ex.printStackTrace();}
}
}

try {
Thread.sleep(5000);
} catch (InterruptedException ex) {ex.printStackTrace();}
}
} else {
System.out.println("No stream file was specified.");
}
}}


Any help would be appreciated.

Answer

As example

Someting like

if (!PRlist.get(i).isAlive()){
    int exitCode=PRlist.get(i).exitValue();
    //Do something with exitValue

    PRlist.get(i).destroy();
    PRlist.get(i).destroy();
    PRlist.remove(i);
    try {
        Process PR = new ProcessBuilder().inheritIO().command(commandsList.get(i)).redirectInput(Redirect.INHERIT).start();
        System.out.println(commandsList.get(i)[12] + " stream re-established at " + LocalDateTime.now().format(frmt));
         PRlist.add(i,PR);
         } catch (IOException ex) {ex.printStackTrace();}
         }

Examples with errorStream may be more complex, but the idea is looking at the exception being posted in errorStream via text processing.