Example usage for org.springframework.batch.core ExitStatus STOPPED

List of usage examples for org.springframework.batch.core ExitStatus STOPPED

Introduction

In this page you can find the example usage for org.springframework.batch.core ExitStatus STOPPED.

Prototype

ExitStatus STOPPED

To view the source code for org.springframework.batch.core ExitStatus STOPPED.

Click Source Link

Document

Convenient constant value representing finished processing with interrupted status.

Usage

From source file:org.springframework.batch.core.job.AbstractJob.java

/**
 * Default mapping from throwable to {@link ExitStatus}.
 *
 * @param ex//from   w  w w. j a  v a2 s  .  co  m
 *            the cause of the failure
 * @return an {@link ExitStatus}
 */
protected ExitStatus getDefaultExitStatusForFailure(Throwable ex, JobExecution execution) {
    ExitStatus exitStatus;
    if (ex instanceof JobInterruptedException || ex.getCause() instanceof JobInterruptedException) {
        exitStatus = ExitStatus.STOPPED.addExitDescription(JobInterruptedException.class.getName());
    } else if (ex instanceof NoSuchJobException || ex.getCause() instanceof NoSuchJobException) {
        exitStatus = new ExitStatus(ExitCodeMapper.NO_SUCH_JOB, ex.getClass().getName());
    } else {
        exitStatus = ExitStatus.FAILED.addExitDescription(ex);
    }

    return exitStatus;
}

From source file:org.springframework.batch.core.step.AbstractStep.java

/**
 * Default mapping from throwable to {@link ExitStatus}. Clients can modify the exit code using a
 * {@link StepExecutionListener}./* w  w  w  . ja  v a  2 s  .c  o  m*/
 *
 * @param ex the cause of the failure
 * @return an {@link ExitStatus}
 */
private ExitStatus getDefaultExitStatusForFailure(Throwable ex) {
    ExitStatus exitStatus;
    if (ex instanceof JobInterruptedException || ex.getCause() instanceof JobInterruptedException) {
        exitStatus = ExitStatus.STOPPED.addExitDescription(JobInterruptedException.class.getName());
    } else if (ex instanceof NoSuchJobException || ex.getCause() instanceof NoSuchJobException) {
        exitStatus = new ExitStatus(ExitCodeMapper.NO_SUCH_JOB, ex.getClass().getName());
    } else {
        exitStatus = ExitStatus.FAILED.addExitDescription(ex);
    }

    return exitStatus;
}

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}.//  w w w.j  av a 2 s. c om
 */
@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;
        }
    }
}

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

@Test
public void testStopped() throws Exception {
    initializeTasklet();//from www  . j ava  2s  . co m
    tasklet.setJobExplorer(jobExplorer);
    tasklet.afterPropertiesSet();
    tasklet.beforeStep(stepExecution);

    JobExecution stoppedJobExecution = new JobExecution(stepExecution.getJobExecution());
    stoppedJobExecution.setStatus(BatchStatus.STOPPING);

    when(jobExplorer.getJobExecution(1L)).thenReturn(stepExecution.getJobExecution(),
            stepExecution.getJobExecution(), stoppedJobExecution);

    String command = System.getProperty("os.name").toLowerCase().contains("win") ? "ping 1.1.1.1 -n 1 -w 5000"
            : "sleep 15";
    tasklet.setCommand(command);
    tasklet.setTerminationCheckInterval(10);
    tasklet.afterPropertiesSet();

    StepContribution contribution = stepExecution.createStepContribution();
    StepContext stepContext = new StepContext(stepExecution);
    ChunkContext chunkContext = new ChunkContext(stepContext);
    tasklet.execute(contribution, chunkContext);

    assertEquals(contribution.getExitStatus().getExitCode(), ExitStatus.STOPPED.getExitCode());
}