Process Watch Dog Released!

It's a good idea to work with external processes to prevent the system from failure especially when the process is very resources-demanding.
But what if something goes wrong?
Let the watch dog out!


Consider a very simple tooling class for executing new processes. Put the watch dog to kill processes after 2 minutes:

import cz.net21.ttulka.exec.ProcessWatchDog;

public class ProcessExecutorTool {

    private static int TIMEOUT = 2 ✱ 60 ✱ 1000; // 2 minutes

    private static ProcessWatchDog watchDog = new ProcessWatchDog();

    public static int executeExternalProcess(String... cmd) {
        try {
            ProcessBuilder builder = new ProcessBuilder(cmd);
            Process process = builder.start();

  , TIMEOUT);   // release the dog!
            return process.exitValue();
        } catch (Exception e) {
            throw new RuntimeException("Exception by executing '" + cmd + "'.", e);

All about:

Happy watching!