Example usage for org.springframework.batch.core StepExecution getReadCount

List of usage examples for org.springframework.batch.core StepExecution getReadCount

Introduction

In this page you can find the example usage for org.springframework.batch.core StepExecution getReadCount.

Prototype

public int getReadCount() 

Source Link

Document

Returns the current number of items read for this execution

Usage

From source file:org.springframework.batch.core.jsr.configuration.xml.RetryListenerTests.java

@Test
@SuppressWarnings("resource")
public void testReadRetryOnce() throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext(
            "org/springframework/batch/core/jsr/configuration/xml/RetryReadListenerRetryOnce.xml");

    JobLauncher jobLauncher = context.getBean(JobLauncher.class);
    JobExecution jobExecution = jobLauncher.run(context.getBean(Job.class), new JobParameters());

    Collection<StepExecution> stepExecutions = jobExecution.getStepExecutions();
    assertEquals(1, stepExecutions.size());

    StepExecution stepExecution = stepExecutions.iterator().next();
    assertEquals(1, stepExecution.getCommitCount());
    assertEquals(2, stepExecution.getReadCount());

    assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
}

From source file:org.springframework.batch.core.repository.dao.JdbcStepExecutionDao.java

private List<Object[]> buildStepExecutionParameters(StepExecution stepExecution) {
    Assert.isNull(stepExecution.getId(),
            "to-be-saved (not updated) StepExecution can't already have an id assigned");
    Assert.isNull(stepExecution.getVersion(),
            "to-be-saved (not updated) StepExecution can't already have a version assigned");
    validateStepExecution(stepExecution);
    stepExecution.setId(stepExecutionIncrementer.nextLongValue());
    stepExecution.incrementVersion(); //Should be 0
    List<Object[]> parameters = new ArrayList<Object[]>();
    String exitDescription = truncateExitDescription(stepExecution.getExitStatus().getExitDescription());
    Object[] parameterValues = new Object[] { stepExecution.getId(), stepExecution.getVersion(),
            stepExecution.getStepName(), stepExecution.getJobExecutionId(), stepExecution.getStartTime(),
            stepExecution.getEndTime(), stepExecution.getStatus().toString(), stepExecution.getCommitCount(),
            stepExecution.getReadCount(), stepExecution.getFilterCount(), stepExecution.getWriteCount(),
            stepExecution.getExitStatus().getExitCode(), exitDescription, stepExecution.getReadSkipCount(),
            stepExecution.getWriteSkipCount(), stepExecution.getProcessSkipCount(),
            stepExecution.getRollbackCount(), stepExecution.getLastUpdated() };
    Integer[] parameterTypes = new Integer[] { Types.BIGINT, Types.INTEGER, Types.VARCHAR, Types.BIGINT,
            Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER,
            Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER,
            Types.INTEGER, Types.TIMESTAMP };
    parameters.add(0, Arrays.copyOf(parameterValues, parameterValues.length));
    parameters.add(1, Arrays.copyOf(parameterTypes, parameterTypes.length));
    return parameters;
}

From source file:org.springframework.batch.core.repository.dao.JdbcStepExecutionDao.java

@Override
public void updateStepExecution(StepExecution stepExecution) {

    validateStepExecution(stepExecution);
    Assert.notNull(stepExecution.getId(),
            "StepExecution Id cannot be null. StepExecution must saved" + " before it can be updated.");

    // Do not check for existence of step execution considering
    // it is saved at every commit point.

    String exitDescription = truncateExitDescription(stepExecution.getExitStatus().getExitDescription());

    // Attempt to prevent concurrent modification errors by blocking here if
    // someone is already trying to do it.
    synchronized (stepExecution) {

        Integer version = stepExecution.getVersion() + 1;
        Object[] parameters = new Object[] { stepExecution.getStartTime(), stepExecution.getEndTime(),
                stepExecution.getStatus().toString(), stepExecution.getCommitCount(),
                stepExecution.getReadCount(), stepExecution.getFilterCount(), stepExecution.getWriteCount(),
                stepExecution.getExitStatus().getExitCode(), exitDescription, version,
                stepExecution.getReadSkipCount(), stepExecution.getProcessSkipCount(),
                stepExecution.getWriteSkipCount(), stepExecution.getRollbackCount(),
                stepExecution.getLastUpdated(), stepExecution.getId(), stepExecution.getVersion() };
        int count = getJdbcTemplate().update(getQuery(UPDATE_STEP_EXECUTION), parameters,
                new int[] { Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.INTEGER, Types.INTEGER,
                        Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.INTEGER,
                        Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.TIMESTAMP,
                        Types.BIGINT, Types.INTEGER });

        // Avoid concurrent modifications...
        if (count == 0) {
            int curentVersion = getJdbcTemplate().queryForObject(getQuery(CURRENT_VERSION_STEP_EXECUTION),
                    new Object[] { stepExecution.getId() }, Integer.class);
            throw new OptimisticLockingFailureException(
                    "Attempt to update step execution id=" + stepExecution.getId() + " with wrong version ("
                            + stepExecution.getVersion() + "), where current version is " + curentVersion);
        }//w  ww  .j  a  v  a2  s  .co  m

        stepExecution.incrementVersion();

    }
}

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

/**
 * Check items causing errors are skipped as expected.
 *//*  w  w w. ja  va2  s  .c o 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

/**
 * 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  w  w w .j ava2  s  . c  om
 *
 * @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);/* ww  w .j a  va 2 s .  co  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  . j  a va 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);// w  w w.  j a  va2 s  . co  m
    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//w ww . j  a v a  2  s.  c  om
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());
}

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

@Test
public void testRetryPolicy() throws Exception {
    factory.setRetryPolicy(new SimpleRetryPolicy(4,
            Collections.<Class<? extends Throwable>, Boolean>singletonMap(Exception.class, true)));
    factory.setSkipLimit(0);/*from  www .ja va  2s .  c  o  m*/
    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);
    AbstractStep step = (AbstractStep) 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());
    assertEquals("[b, b, b, b, b]", processed.toString());
    // []
    assertEquals(0, recovered.size());
    assertEquals(1, stepExecution.getReadCount());
}