Example usage for org.springframework.batch.core.step.tasklet SystemCommandException SystemCommandException

List of usage examples for org.springframework.batch.core.step.tasklet SystemCommandException SystemCommandException

Introduction

In this page you can find the example usage for org.springframework.batch.core.step.tasklet SystemCommandException SystemCommandException.

Prototype

public SystemCommandException(String message) 

Source Link

Usage

From source file:org.springframework.batch.core.step.tasklet.SystemCommandTasklet.java

/**
 * Execute system command and map its exit code to {@link ExitStatus} using
 * {@link SystemProcessExitCodeMapper}.//from  w ww  .j  a va  2s .  com
 */
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

    FutureTask<Integer> systemCommandTask = new FutureTask<Integer>(new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            Process process = Runtime.getRuntime().exec(command, environmentParams, workingDirectory);
            return process.waitFor();
        }

    });

    long t0 = System.currentTimeMillis();

    taskExecutor.execute(systemCommandTask);

    while (true) {
        Thread.sleep(checkInterval);//moved to the end of the logic

        if (stoppable) {
            JobExecution jobExecution = jobExplorer
                    .getJobExecution(chunkContext.getStepContext().getStepExecution().getJobExecutionId());

            if (jobExecution.isStopping()) {
                stopped = true;
            }
        }

        if (systemCommandTask.isDone()) {
            contribution.setExitStatus(systemProcessExitCodeMapper.getExitStatus(systemCommandTask.get()));
            return RepeatStatus.FINISHED;
        } else if (System.currentTimeMillis() - t0 > timeout) {
            systemCommandTask.cancel(interruptOnCancel);
            throw new SystemCommandException("Execution of system command did not finish within the timeout");
        } else if (execution.isTerminateOnly()) {
            systemCommandTask.cancel(interruptOnCancel);
            throw new JobInterruptedException(
                    "Job interrupted while executing system command '" + command + "'");
        } else if (stopped) {
            systemCommandTask.cancel(interruptOnCancel);
            contribution.setExitStatus(ExitStatus.STOPPED);
            return RepeatStatus.FINISHED;
        }
    }
}