Example usage for org.apache.commons.exec ExecuteWatchdog ExecuteWatchdog

List of usage examples for org.apache.commons.exec ExecuteWatchdog ExecuteWatchdog

Introduction

In this page you can find the example usage for org.apache.commons.exec ExecuteWatchdog ExecuteWatchdog.

Prototype

public ExecuteWatchdog(final long timeout) 

Source Link

Document

Creates a new watchdog with a given timeout.

Usage

From source file:ro.cosu.vampires.server.resources.local.LocalResource.java

private void execute(CommandLine cmd) throws IOException {
    executor.setWorkingDirectory(Paths.get("").toAbsolutePath().toFile());
    executor.setStreamHandler(new PumpStreamHandler(collectingLogOutputStream));
    // watchdog for 3 hours. a bit excessive ?
    executor.setWatchdog(new ExecuteWatchdog(1000 * 60 * 60 * 3));

    int exitCode = 0;
    try {//from   w w w .j a  v a2s .c o m
        LOG.debug("execute {}", cmd.toString());
        exitCode = executor.execute(cmd);
        if (exitCode != 0) {
            LOG.error("Output {}", collectingLogOutputStream.getLines());
            throw new IOException("Non zero exit code");
        }
    } catch (IOException e) {
        LOG.debug("{} has failed with error {}: {}", this, exitCode, e);
        LOG.debug("{}", Joiner.on("\n").join(collectingLogOutputStream.getLines()));
        throw e;
    }
}

From source file:sce.ProcessExecutor.java

public String executeProcess(String[] processParameters) throws JobExecutionException {
    try {//from w  w  w.ja va2 s  . com
        //Command to be executed
        CommandLine command = new CommandLine(processParameters[0]);

        String[] params = new String[processParameters.length - 1];
        for (int i = 0; i < processParameters.length - 1; i++) {
            params[i] = processParameters[i + 1];
        }

        //Adding its arguments
        command.addArguments(params);

        //set timeout in seconds
        ExecuteWatchdog watchDog = new ExecuteWatchdog(
                this.timeout == 0 ? ExecuteWatchdog.INFINITE_TIMEOUT : this.timeout * 1000);
        this.watchdog = watchDog;

        //Result Handler for executing the process in a Asynch way
        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
        //MyResultHandler resultHandler = new MyResultHandler();

        //Using Std out for the output/error stream
        //ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        //This is used to end the process when the JVM exits
        ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();

        //Our main command executor
        DefaultExecutor executor = new DefaultExecutor();

        //Setting the properties
        executor.setStreamHandler(new PumpStreamHandler(null, null));
        executor.setWatchdog(watchDog);
        //executor.setExitValue(1); // this has to be set if the java code contains System.exit(1) to avoid a FAILED status

        //Setting the working directory
        //Use of recursion along with the ls makes this a long running process
        //executor.setWorkingDirectory(new File("/home"));
        executor.setProcessDestroyer(processDestroyer);

        //if set, use the java environment variables when running the command
        if (!this.environment.equals("")) {
            Map<String, String> procEnv = EnvironmentUtils.getProcEnvironment();
            EnvironmentUtils.addVariableToEnvironment(procEnv, this.environment);
            //Executing the command
            executor.execute(command, procEnv, resultHandler);
        } else {
            //Executing the command
            executor.execute(command, resultHandler);
        }

        //The below section depends on your need
        //Anything after this will be executed only when the command completes the execution
        resultHandler.waitFor();

        /*int exitValue = resultHandler.getExitValue();
         System.out.println(exitValue);
         if (executor.isFailure(exitValue)) {
         System.out.println("Execution failed");
         } else {
         System.out.println("Execution Successful");
         }
         System.out.println(outputStream.toString());*/
        //return outputStream.toString();
        if (watchdog.killedProcess()) {
            throw new JobExecutionException("Job Interrupted", new InterruptedException());
        }
        if (executor.isFailure(resultHandler.getExitValue())) {
            ExecuteException ex = resultHandler.getException();
            throw new JobExecutionException(ex.getMessage(), ex);
        }
        return "1";
    } catch (ExecuteException ex) {
        throw new JobExecutionException(ex.getMessage(), ex);
    } catch (IOException | InterruptedException | JobExecutionException ex) {
        throw new JobExecutionException(ex.getMessage(), ex);
    }
}

From source file:uk.org.sappho.applications.transcript.service.registry.vcs.CommandExecuter.java

public String execute(Command command, File directory) throws TranscriptException {

    try {//ww  w  .  j a  v  a 2  s.  co  m
        if (logger.isLoggable(Level.INFO)) {
            logger.info(command.getSafeCommand());
        }
        DefaultExecutor executor = new DefaultExecutor();
        DefaultExecuteResultHandler commandResultsHandler = new DefaultExecuteResultHandler();
        ByteArrayOutputStream commandOutputStream = new ByteArrayOutputStream();
        ByteArrayOutputStream commandErrorStream = new ByteArrayOutputStream();
        PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(commandOutputStream, commandErrorStream);
        executor.setWatchdog(new ExecuteWatchdog(60000));
        executor.setStreamHandler(pumpStreamHandler);
        executor.setWorkingDirectory(directory);
        executor.execute(command.getCommandLine(), commandResultsHandler);
        commandResultsHandler.waitFor();
        if (commandResultsHandler.getExitValue() != 0) {
            throw new TranscriptException(commandErrorStream.toString());
        }
        return commandOutputStream.toString();
    } catch (Throwable throwable) {
        if (logger.isLoggable(Level.WARNING)) {
            logger.warning(throwable.getMessage());
        }
        throw new TranscriptException("Unable to execute system command: " + command.getSafeCommand(),
                throwable);
    }
}