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

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

Introduction

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

Prototype

public StepExecution(String stepName, JobExecution jobExecution) 

Source Link

Document

Constructor that substitutes in null for the execution id

Usage

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

@Test
public void testRestartAfterFailedWrite() throws Exception {

    factory.setSkipLimit(0);/*w w  w . j ava 2 s .c  om*/
    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);/*from ww  w. ja  v a 2 s .c  o  m*/
    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

@SuppressWarnings("unchecked")
@Test/*from  w  w  w .  j  ava 2  s .c om*/
public void testSkipAndRetryWithWriteFailure() throws Exception {

    factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() {
        @Override
        public void onSkipInWrite(String item, Throwable t) {
            recovered.add(item);
            assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
        }
    } });
    factory.setSkipLimit(2);
    ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("a", "b", "c", "d", "e", "f")) {
        @Override
        public String read() {
            String item = super.read();
            logger.debug("Read Called! Item: [" + item + "]");
            provided.add(item);
            count++;
            return item;
        }
    };

    ItemWriter<String> itemWriter = new ItemWriter<String>() {
        @Override
        public void write(List<? extends String> item) throws Exception {
            logger.debug("Write Called! Item: [" + item + "]");
            processed.addAll(item);
            written.addAll(item);
            if (item.contains("b") || item.contains("d")) {
                throw new RuntimeException("Write error - planned but recoverable.");
            }
        }
    };
    factory.setItemReader(provider);
    factory.setItemWriter(itemWriter);
    factory.setRetryLimit(5);
    factory.setRetryableExceptionClasses(getExceptionMap(RuntimeException.class));
    AbstractStep step = (AbstractStep) factory.getObject();
    step.setName("mytest");
    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
    repository.add(stepExecution);
    step.execute(stepExecution);

    assertEquals(2, recovered.size());
    assertEquals(2, stepExecution.getSkipCount());
    assertEquals(2, stepExecution.getWriteSkipCount());

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

    assertEquals("[a, b, c, d, e, f, null]", provided.toString());
    assertEquals("[a, b, b, b, b, b, b, c, d, d, d, d, d, d, e, f]", processed.toString());
    assertEquals("[b, d]", recovered.toString());
}

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

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

    factory.setCommitInterval(3);
    factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() {
        @Override
        public void onSkipInWrite(String item, Throwable t) {
            recovered.add(item);
            assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
        }
    } });
    factory.setSkipLimit(2);
    ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("a", "b", "c", "d", "e", "f")) {
        @Override
        public String read() {
            String item = super.read();
            logger.debug("Read Called! Item: [" + item + "]");
            provided.add(item);
            count++;
            return item;
        }
    };

    ItemWriter<String> itemWriter = new ItemWriter<String>() {
        @Override
        public void write(List<? extends String> item) throws Exception {
            logger.debug("Write Called! Item: [" + item + "]");
            processed.addAll(item);
            written.addAll(item);
            if (item.contains("b") || item.contains("d")) {
                throw new RuntimeException("Write error - planned but recoverable.");
            }
        }
    };
    factory.setItemReader(provider);
    factory.setItemWriter(itemWriter);
    factory.setRetryLimit(5);
    factory.setRetryableExceptionClasses(getExceptionMap(RuntimeException.class));
    AbstractStep step = (AbstractStep) factory.getObject();
    step.setName("mytest");
    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
    repository.add(stepExecution);
    step.execute(stepExecution);

    assertEquals(2, recovered.size());
    assertEquals(2, stepExecution.getSkipCount());
    assertEquals(2, stepExecution.getWriteSkipCount());

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

    // [a, b, c, d, e, f, null]
    assertEquals(7, provided.size());
    // [a, b, c, a, b, c, a, b, c, a, b, c, a, b, c, a, b, c, d, e, f, d,
    // e, f, d, e, f, d, e, f, d, e, f, d, e, f]
    // System.err.println(processed);
    assertEquals(36, processed.size());
    // [b, d]
    assertEquals(2, recovered.size());
}

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

@Test
public void testRetryWithNoSkip() throws Exception {

    factory.setRetryLimit(4);//from   w  w  w.  j a  va 2  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 av  a2  s .  co  m
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);/*ww w  . j  a va 2  s  . 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());
}

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

@Test
public void testCacheLimitWithRetry() throws Exception {
    factory.setRetryLimit(2);/* w w  w  .ja  va 2  s  . c  o m*/
    factory.setCommitInterval(3);
    // sufficiently high so we never hit it
    factory.setSkipLimit(10);
    // set the cache limit stupidly low
    factory.setRetryContextCache(new MapRetryContextCache(0));
    ItemReader<String> provider = new ItemReader<String>() {
        @Override
        public String read() {
            String item = "" + count;
            provided.add(item);
            count++;
            if (count >= 10) {
                // prevent infinite loop in worst case scenario
                return null;
            }
            return item;
        }
    };
    ItemWriter<String> itemWriter = new ItemWriter<String>() {
        @Override
        public void write(List<? extends String> item) throws Exception {
            processed.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());

    // We added a bogus cache so no items are actually skipped
    // because they aren't recognised as eligible
    assertEquals(0, stepExecution.getSkipCount());
    // [0, 1, 2]
    assertEquals(3, provided.size());
    // [0, 1, 2]
    assertEquals(3, processed.size());
    // []
    assertEquals(0, recovered.size());
}

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

@Test
@Ignore//from   ww  w.jav  a  2  s  . co  m
public void testStatus() throws Exception {

    step.setTasklet(new TestingChunkOrientedTasklet<String>(
            getReader(new String[] { "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c" }),
            new ItemWriter<String>() {
                @Override
                public void write(List<? extends String> data) throws Exception {
                    written.addAll(data);
                }
            }, chunkOperations));

    final JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters(
            Collections.singletonMap("run.id", new JobParameter(getClass().getName() + ".1"))));
    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);

    jobRepository.add(stepExecution);
    step.execute(stepExecution);
    assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
    // Need a transaction so one connection is enough to get job execution and its parameters
    StepExecution lastStepExecution = new TransactionTemplate(transactionManager)
            .execute(new TransactionCallback<StepExecution>() {
                @Override
                public StepExecution doInTransaction(TransactionStatus status) {
                    return jobRepository.getLastStepExecution(jobExecution.getJobInstance(), step.getName());
                }
            });
    assertEquals(lastStepExecution, stepExecution);
    assertFalse(lastStepExecution == stepExecution);
}