1

I am facing a problem regarding Process and Threads. My Scenario is:

  • My Java Application, call it 'Starter-App', starts another exe Application (Diagnosis.exe) with ProcessBuilder within a named Thread:

    Thread startGXThread = new Thread(new Runnable() {  
        @Override       
        public void run() {
            try {
                ...
                File gxExe = new File(pathToGX); // Path to Diagnosis.exe
                gxp = pb.start();
                gxp.waitFor();
    
            } catch (IOException e) {
                LOG.error("Can't start module");
                LOG.error(e.getMessage(), e);
            } catch (InterruptedException e) {
                LOG.debug("thread interrupted. Destroy process");
                LOG.debug(e.getMessage(), e);
                if (gxp != null) {
                    gxp.destroy();
                    LOG.debug("process exit value: " + gxp.exitValue());
                }
            }
        }
    }, "diag_thrd");
    
  • Afterwards a jetty webserver (ServiceWebApp) is started with a webapp.

  • start chromium and 'Starter-App' listen when its closed.
  • Once chromium closes 'Starter-App' recognizes this and stops jetty and also terminate the startet application.Diagnosis.exe. This is done by:

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            stopAsync();
        }
    });
    
    public static void stopAsync() {
        Thread diag = getThread("diag_thrd");
        if (diag != null) {
            diag.interrupt();
        }
        if (gxp != null) {
            gxp.destroy();
            LOG.debug("process exit value: " + gxp.exitValue());
        }
    }
    

Question: I need to be able to stop the startet Diagnosis.exe from within the webapp and start it again, while still be able to destroy/shutdown the Diagnosis.exe once chromium stops within 'Starter-App'. I hope I could explain my problem and hope for suggestions.

kism3t
  • 1,343
  • 1
  • 14
  • 33
  • So the Starter-App starts Diagnosis.exe, a jetty server and Chromium? – Sonata Dec 20 '16 at 09:38
  • That is right. The 'Starter-App' starts Diagnosis.exe, Jetty (with a deployed webapp), and Chromium (with url to the webapp). Once Chromium closes (by user) 'Starter-App' recognizes this and shuts down jetty and Diagnosis.exe. – kism3t Dec 20 '16 at 09:47

2 Answers2

2

Building on Anands answer, I think you need some form of IPC between the Diagnosis.exe and your Starter-App, using websockets, or a number of other options, for some ideas: How to have 2 JVMs talk to one another.

The webapp would send a request for restarting Diagnosis.exe to the Starter-App and the Starter-App would stay in charge of managing the application trio at all time.

Community
  • 1
  • 1
Sonata
  • 2,177
  • 25
  • 32
  • Liked this idea - The webapp would send a request for restarting Diagnosis.exe to the Starter-App and the Starter-App would stay in charge of managing the application trio at all time. – Anand Vaidya Dec 20 '16 at 10:15
1

I think there is a solution but a but tricky to implement.

You can always use *nix api's like ps kill #pid as explained in the example here Killing a process using Java

But your webserver has to know which PID's to look for. The only option I see to implement such thing is using sockets or webservices. So you keep track of what is the current pid of Diagnosis.exe process, and use that Id before killing.

Community
  • 1
  • 1
Anand Vaidya
  • 1,374
  • 11
  • 26
  • I am using windows. So it would probably not be `ps kill`. I could pass the pid as a system paramter to the webapp but once the web app is shut down the 'starter-app's reference is not valid anymore. – kism3t Dec 20 '16 at 09:32
  • Yeah. Windows will have different option. Also I would not recommend sending PID as system parameter, but that should be sent as a heart-beat or some RMI/Webservice call. because There is a possibility of webserver restarting the process. So whosoever restarts the Diagnostic.exe notifies other of the new process Id to look for. – Anand Vaidya Dec 20 '16 at 09:35