Example usage for org.springframework.batch.core Step execute

List of usage examples for org.springframework.batch.core Step execute

Introduction

In this page you can find the example usage for org.springframework.batch.core Step execute.

Prototype

void execute(StepExecution stepExecution) throws JobInterruptedException;

Source Link

Document

Process the step and assign progress and status meta information to the StepExecution provided.

Usage

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

@Override
public StepExecution handleStep(Step step, JobExecution execution)
        throws JobInterruptedException, JobRestartException, StartLimitExceededException {
    if (execution.isStopping()) {
        throw new JobInterruptedException("JobExecution interrupted.");
    }// w w  w  . j a v a 2 s .  co m

    JobInstance jobInstance = execution.getJobInstance();

    StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, step.getName());
    if (stepExecutionPartOfExistingJobExecution(execution, lastStepExecution)) {
        // If the last execution of this step was in the same job, it's
        // probably intentional so we want to run it again...
        logger.info(String.format(
                "Duplicate step [%s] detected in execution of job=[%s]. "
                        + "If either step fails, both will be executed again on restart.",
                step.getName(), jobInstance.getJobName()));
        lastStepExecution = null;
    }
    StepExecution currentStepExecution = lastStepExecution;

    if (shouldStart(lastStepExecution, execution, step)) {

        currentStepExecution = execution.createStepExecution(step.getName());

        boolean isRestart = (lastStepExecution != null
                && !lastStepExecution.getStatus().equals(BatchStatus.COMPLETED));

        if (isRestart) {
            currentStepExecution.setExecutionContext(lastStepExecution.getExecutionContext());

            if (lastStepExecution.getExecutionContext().containsKey("batch.executed")) {
                currentStepExecution.getExecutionContext().remove("batch.executed");
            }
        } else {
            currentStepExecution.setExecutionContext(new ExecutionContext(executionContext));
        }

        jobRepository.add(currentStepExecution);

        logger.info("Executing step: [" + step.getName() + "]");
        try {
            step.execute(currentStepExecution);
            currentStepExecution.getExecutionContext().put("batch.executed", true);
        } catch (JobInterruptedException e) {
            // Ensure that the job gets the message that it is stopping
            // and can pass it on to other steps that are executing
            // concurrently.
            execution.setStatus(BatchStatus.STOPPING);
            throw e;
        }

        jobRepository.updateExecutionContext(execution);

        if (currentStepExecution.getStatus() == BatchStatus.STOPPING
                || currentStepExecution.getStatus() == BatchStatus.STOPPED) {
            // Ensure that the job gets the message that it is stopping
            execution.setStatus(BatchStatus.STOPPING);
            throw new JobInterruptedException("Job interrupted by step execution");
        }

    }

    return currentStepExecution;
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanNonBufferingTests.java

/**
 * Check items causing errors are skipped as expected.
 *///  w  w  w .  j  a  va 2  s  . co m
@Test
public void testSkip() throws Exception {
    @SuppressWarnings("unchecked")
    SkipListener<Integer, String> skipListener = mock(SkipListener.class);
    skipListener.onSkipInWrite("3", exception);
    skipListener.onSkipInWrite("4", exception);

    factory.setListeners(new SkipListener[] { skipListener });
    Step step = factory.getObject();

    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
    step.execute(stepExecution);

    assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());

    assertEquals(2, stepExecution.getSkipCount());
    assertEquals(0, stepExecution.getReadSkipCount());
    assertEquals(2, stepExecution.getWriteSkipCount());

    // only one exception caused rollback, and only once in this case
    // because all items in that chunk were skipped immediately
    assertEquals(1, stepExecution.getRollbackCount());

    assertFalse(writer.written.contains("4"));

    List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,2,5"));
    assertEquals(expectedOutput, writer.written);

    // 5 items + 1 rollbacks reading 2 items each time
    assertEquals(7, stepExecution.getReadCount());

}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java

@Test
public void testProcessAllItemsWhenErrorInWriterTransformationWhenReaderTransactional() throws Exception {
    final int RETRY_LIMIT = 3;
    final List<String> ITEM_LIST = TransactionAwareProxyFactory
            .createTransactionalList(Arrays.asList("1", "2", "3"));
    FaultTolerantStepFactoryBean<String, Integer> factory = new FaultTolerantStepFactoryBean<String, Integer>();
    factory.setBeanName("step");

    factory.setJobRepository(repository);
    factory.setTransactionManager(new ResourcelessTransactionManager());
    ItemWriter<Integer> failingWriter = new ItemWriter<Integer>() {
        @Override/*from w  ww .  j av a2s  .co m*/
        public void write(List<? extends Integer> data) throws Exception {
            int count = 0;
            for (Integer item : data) {
                if (count++ == 2) {
                    throw new Exception("Planned failure in writer");
                }
                written.add(item);
            }
        }
    };

    ItemProcessor<String, Integer> processor = new ItemProcessor<String, Integer>() {
        @Override
        public Integer process(String item) throws Exception {
            processed.add(item);
            return Integer.parseInt(item);
        }
    };
    ItemReader<String> reader = new ListItemReader<String>(
            TransactionAwareProxyFactory.createTransactionalList(ITEM_LIST));
    factory.setCommitInterval(3);
    factory.setRetryLimit(RETRY_LIMIT);
    factory.setSkipLimit(1);
    factory.setIsReaderTransactionalQueue(true);
    @SuppressWarnings("unchecked")
    Map<Class<? extends Throwable>, Boolean> exceptionMap = getExceptionMap(Exception.class);
    factory.setSkippableExceptionClasses(exceptionMap);
    factory.setRetryableExceptionClasses(exceptionMap);
    factory.setItemReader(reader);
    factory.setItemProcessor(processor);
    factory.setItemWriter(failingWriter);
    Step step = factory.getObject();

    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
    repository.add(stepExecution);
    step.execute(stepExecution);
    /*
     * Each chunk tried up to RETRY_LIMIT, then the scan processes each item
     * once, identifying the skip as it goes
     */
    assertEquals((RETRY_LIMIT + 1) * ITEM_LIST.size(), processed.size());
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java

@Test
public void testProcessAllItemsWhenErrorInWriter() throws Exception {
    final int RETRY_LIMIT = 3;
    final List<String> ITEM_LIST = Arrays.asList("a", "b", "c");
    ItemWriter<String> failingWriter = new ItemWriter<String>() {
        @Override//from   w ww  .ja v a2 s . c  o  m
        public void write(List<? extends String> data) throws Exception {
            int count = 0;
            for (String item : data) {
                if (count++ == 2) {
                    throw new Exception("Planned failure in writer");
                }
                written.add(item);
            }
        }
    };

    ItemProcessor<String, String> processor = new ItemProcessor<String, String>() {
        @Override
        public String process(String item) throws Exception {
            processed.add(item);
            return item;
        }
    };
    ItemReader<String> reader = new ListItemReader<String>(ITEM_LIST);
    factory.setCommitInterval(3);
    factory.setRetryLimit(RETRY_LIMIT);
    factory.setSkipLimit(1);
    @SuppressWarnings("unchecked")
    Map<Class<? extends Throwable>, Boolean> exceptionMap = getExceptionMap(Exception.class);
    factory.setSkippableExceptionClasses(exceptionMap);
    factory.setItemReader(reader);
    factory.setItemProcessor(processor);
    factory.setItemWriter(failingWriter);
    Step step = factory.getObject();

    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
    repository.add(stepExecution);
    step.execute(stepExecution);
    assertEquals(ExitStatus.COMPLETED.getExitCode(), stepExecution.getExitStatus().getExitCode());
    /*
     * Each chunk tried up to RETRY_LIMIT, then the scan processes each item
     * once, identifying the skip as it goes
     */
    assertEquals((RETRY_LIMIT + 1) * ITEM_LIST.size(), processed.size());
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java

@Test
public void testNoItemsReprocessedWhenErrorInWriterAndProcessorNotTransactional() throws Exception {
    ItemWriter<String> failingWriter = new ItemWriter<String>() {
        @Override/*from   www.ja  v  a  2 s.c o m*/
        public void write(List<? extends String> data) throws Exception {
            int count = 0;
            for (String item : data) {
                if (count++ == 2) {
                    throw new Exception("Planned failure in writer");
                }
                written.add(item);
            }
        }
    };

    ItemProcessor<String, String> processor = new ItemProcessor<String, String>() {
        @Override
        public String process(String item) throws Exception {
            processed.add(item);
            return item;
        }
    };
    ItemReader<String> reader = new ListItemReader<String>(Arrays.asList("a", "b", "c"));
    factory.setProcessorTransactional(false);
    factory.setCommitInterval(3);
    factory.setRetryLimit(3);
    factory.setSkippableExceptionClasses(new HashMap<Class<? extends Throwable>, Boolean>());
    factory.setItemReader(reader);
    factory.setItemProcessor(processor);
    factory.setItemWriter(failingWriter);
    Step step = factory.getObject();

    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
    repository.add(stepExecution);
    step.execute(stepExecution);
    assertEquals(3, processed.size()); // Initial try only, then cached
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java

/**
 * N.B. this doesn't really test retry, since the retry is only on write
 * failures, but it does test that read errors are re-presented for another
 * try when the retryLimit is high enough (it is used to build an exception
 * handler)./*from   ww  w.  ja  v a2s .  c  o  m*/
 *
 * @throws Exception
 */
@SuppressWarnings("unchecked")
@Test
public void testSuccessfulRetryWithReadFailure() throws Exception {
    ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("a", "b", "c")) {
        @Override
        public String read() {
            String item = super.read();
            provided.add(item);
            count++;
            if (count == 2) {
                throw new RuntimeException("Temporary error - retry for success.");
            }
            return item;
        }
    };
    factory.setItemReader(provider);
    factory.setRetryLimit(10);
    factory.setSkippableExceptionClasses(getExceptionMap());
    Step step = factory.getObject();

    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
    repository.add(stepExecution);
    step.execute(stepExecution);

    assertEquals(0, stepExecution.getSkipCount());

    // [a, b with error]
    assertEquals(2, provided.size());
    // [a]
    assertEquals(1, processed.size());
    // []
    assertEquals(0, recovered.size());
    assertEquals(1, stepExecution.getReadCount());
    assertEquals(0, stepExecution.getReadSkipCount());
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java

@Test
public void testRestartAfterFailedWrite() throws Exception {

    factory.setSkipLimit(0);// w  w  w. j  a  va 2 s. c  o  m
    factory.setCommitInterval(3);
    AbstractItemCountingItemStreamItemReader<String> reader = new AbstractItemCountingItemStreamItemReader<String>() {

        private ItemReader<String> reader;

        @Override
        protected void doClose() throws Exception {
            reader = null;
        }

        @Override
        protected void doOpen() throws Exception {
            reader = new ListItemReader<String>(Arrays.asList("a", "b", "c", "d", "e", "f"));
        }

        @Override
        protected String doRead() throws Exception {
            return reader.read();
        }

    };
    // Need to set name or else reader will fail to open
    reader.setName("foo");
    factory.setItemReader(reader);
    factory.setStreams(new ItemStream[] { reader });
    factory.setItemWriter(new ItemWriter<String>() {
        @Override
        public void write(List<? extends String> items) throws Exception {
            if (fail && items.contains("e")) {
                throw new RuntimeException("Planned failure");
            }
            processed.addAll(items);
        }
    });
    factory.setRetryLimit(0);
    Step step = factory.getObject();

    fail = true;
    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
    repository.add(stepExecution);
    step.execute(stepExecution);

    assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
    assertEquals(4, stepExecution.getWriteCount());
    assertEquals(6, stepExecution.getReadCount());

    fail = false;
    ExecutionContext executionContext = stepExecution.getExecutionContext();
    stepExecution = new StepExecution(step.getName(), jobExecution);
    stepExecution.setExecutionContext(executionContext);
    repository.add(stepExecution);
    step.execute(stepExecution);

    assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
    assertEquals(2, stepExecution.getWriteCount());
    assertEquals(2, stepExecution.getReadCount());
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java

@Test
public void testSkipAndRetry() throws Exception {

    factory.setSkipLimit(2);/* ww w  . ja  v  a 2 s.  c om*/
    ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("a", "b", "c", "d", "e", "f")) {
        @Override
        public String read() {
            String item = super.read();
            count++;
            if ("b".equals(item) || "d".equals(item)) {
                throw new RuntimeException("Read error - planned but skippable.");
            }
            return item;
        }
    };
    factory.setItemReader(provider);
    factory.setRetryLimit(10);
    Step step = factory.getObject();

    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
    repository.add(stepExecution);
    step.execute(stepExecution);

    assertEquals(2, stepExecution.getSkipCount());
    // b is processed once and skipped, plus 1, plus c, plus the null at end
    assertEquals(7, count);
    assertEquals(4, stepExecution.getReadCount());
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java

@Test
public void testRetryWithNoSkip() throws Exception {

    factory.setRetryLimit(4);/*from  ww w. ja v  a2 s . com*/
    factory.setSkipLimit(0);
    ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("b")) {
        @Override
        public String read() {
            String item = super.read();
            provided.add(item);
            count++;
            return item;
        }
    };
    ItemWriter<String> itemWriter = new ItemWriter<String>() {
        @Override
        public void write(List<? extends String> item) throws Exception {
            processed.addAll(item);
            written.addAll(item);
            logger.debug("Write Called! Item: [" + item + "]");
            throw new RuntimeException("Write error - planned but retryable.");
        }
    };
    factory.setItemReader(provider);
    factory.setItemWriter(itemWriter);
    Step step = factory.getObject();

    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
    repository.add(stepExecution);
    step.execute(stepExecution);
    assertEquals(BatchStatus.FAILED, stepExecution.getStatus());

    List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray(""));
    assertEquals(expectedOutput, written);

    assertEquals(0, stepExecution.getSkipCount());
    // [b]
    assertEquals(1, provided.size());
    // the failed items are tried up to the limit (but only precisely so if
    // the commit interval is 1)
    assertEquals("[b, b, b, b, b]", processed.toString());
    // []
    assertEquals(0, recovered.size());
    assertEquals(1, stepExecution.getReadCount());
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java

@SuppressWarnings("unchecked")
@Test/*from  w  w w  .  j  a v  a  2 s  . com*/
public void testNonSkippableException() throws Exception {

    // Very specific skippable exception
    factory.setSkippableExceptionClasses(getExceptionMap(UnsupportedOperationException.class));
    // ...which is not retryable...
    factory.setRetryableExceptionClasses(getExceptionMap());

    factory.setSkipLimit(1);
    ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("b")) {
        @Override
        public String read() {
            String item = super.read();
            provided.add(item);
            count++;
            return item;
        }
    };
    ItemWriter<String> itemWriter = new ItemWriter<String>() {
        @Override
        public void write(List<? extends String> item) throws Exception {
            processed.addAll(item);
            written.addAll(item);
            logger.debug("Write Called! Item: [" + item + "]");
            throw new RuntimeException("Write error - planned but not skippable.");
        }
    };
    factory.setItemReader(provider);
    factory.setItemWriter(itemWriter);
    Step step = factory.getObject();

    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
    repository.add(stepExecution);
    step.execute(stepExecution);
    String message = stepExecution.getFailureExceptions().get(0).getMessage();
    assertTrue("Wrong message: " + message, message.contains("Write error - planned but not skippable."));

    List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray(""));
    assertEquals(expectedOutput, written);

    assertEquals(0, stepExecution.getSkipCount());
    // [b]
    assertEquals("[b]", provided.toString());
    // [b]
    assertEquals("[b]", processed.toString());
    // []
    assertEquals(0, recovered.size());
    assertEquals(1, stepExecution.getReadCount());
}