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!


<dependency>
    <groupId>cz.net21.ttulka.exec</groupId>
    <artifactId>process-watch-dog</artifactId>
    <version>1.0.0</version>
</dependency>

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();

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

All about: https://github.com/ttulka/process-watch-dog

Happy watching!