List of usage examples for org.springframework.batch.core Step getName
String getName();
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 ww .j av a 2 s. c om*/ 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.job.SimpleStepHandler.java
/** * Given a step and configuration, return true if the step should start, * false if it should not, and throw an exception if the job should finish. * @param lastStepExecution the last step execution * @param jobExecution/*from www . j a v a2 s.c o m*/ * @param step * * @throws StartLimitExceededException if the start limit has been exceeded * for this step * @throws JobRestartException if the job is in an inconsistent state from * an earlier failure */ protected boolean shouldStart(StepExecution lastStepExecution, JobExecution jobExecution, Step step) throws JobRestartException, StartLimitExceededException { BatchStatus stepStatus; if (lastStepExecution == null) { stepStatus = BatchStatus.STARTING; } else { stepStatus = lastStepExecution.getStatus(); } if (stepStatus == BatchStatus.UNKNOWN) { throw new JobRestartException("Cannot restart step from UNKNOWN status. " + "The last execution ended with a failure that could not be rolled back, " + "so it may be dangerous to proceed. Manual intervention is probably necessary."); } if ((stepStatus == BatchStatus.COMPLETED && !step.isAllowStartIfComplete()) || stepStatus == BatchStatus.ABANDONED) { // step is complete, false should be returned, indicating that the // step should not be started logger.info("Step already complete or not restartable, so no action to execute: " + lastStepExecution); return false; } if (jobRepository.getStepExecutionCount(jobExecution.getJobInstance(), step.getName()) < step .getStartLimit()) { // step start count is less than start max, return true return true; } else { // start max has been exceeded, throw an exception. throw new StartLimitExceededException("Maximum start limit exceeded for step: " + step.getName() + "StartMax: " + step.getStartLimit()); } }
From source file:org.springframework.batch.core.jsr.job.DefaultStepHandler.java
/** * Given a step and configuration, return true if the step should start, * false if it should not, and throw an exception if the job should finish. * @param lastStepExecution the last step execution * @param jobInstance/* w w w . j ava2 s .c om*/ * @param step * * @throws StartLimitExceededException if the start limit has been exceeded * for this step * @throws JobRestartException if the job is in an inconsistent state from * an earlier failure */ @Override protected boolean shouldStart(StepExecution lastStepExecution, JobExecution jobExecution, Step step) throws JobRestartException, StartLimitExceededException { BatchStatus stepStatus; String restartStep = null; if (lastStepExecution == null) { jobExecution.getExecutionContext().put("batch.startedStep", step.getName()); stepStatus = BatchStatus.STARTING; } else { stepStatus = lastStepExecution.getStatus(); JobExecution lastJobExecution = getLastJobExecution(jobExecution); if (lastJobExecution.getExecutionContext().containsKey("batch.restartStep")) { restartStep = lastJobExecution.getExecutionContext().getString("batch.restartStep"); if (CollectionUtils.isEmpty(jobExecution.getStepExecutions()) && lastJobExecution.getStatus() == BatchStatus.STOPPED && StringUtils.hasText(restartStep)) { if (!restartStep.equals(step.getName()) && !jobExecution.getExecutionContext().containsKey("batch.startedStep")) { logger.info("Job was stopped and should restart at step " + restartStep + ". The current step is " + step.getName()); return false; } else { // Indicates the starting point for execution evaluation per JSR-352 jobExecution.getExecutionContext().put("batch.startedStep", step.getName()); } } } } if (stepStatus == BatchStatus.UNKNOWN) { throw new JobRestartException("Cannot restart step from UNKNOWN status. " + "The last execution ended with a failure that could not be rolled back, " + "so it may be dangerous to proceed. Manual intervention is probably necessary."); } if ((stepStatus == BatchStatus.COMPLETED && step.isAllowStartIfComplete() == false) || stepStatus == BatchStatus.ABANDONED) { // step is complete, false should be returned, indicating that the // step should not be started logger.info("Step already complete or not restartable, so no action to execute: " + lastStepExecution); return false; } if (getJobRepository().getStepExecutionCount(jobExecution.getJobInstance(), step.getName()) < step .getStartLimit()) { // step start count is less than start max, return true return true; } else { // start max has been exceeded, throw an exception. throw new StartLimitExceededException("Maximum start limit exceeded for step: " + step.getName() + "StartMax: " + step.getStartLimit()); } }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanNonBufferingTests.java
/** * Check items causing errors are skipped as expected. */// w w w . ja va 2s .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
@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 www .j a v a 2s .c o 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 . jav a 2s . 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/* www . jav a 2s .co 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).//www. j a v a 2 s . 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);//from ww w . j ava 2s . 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);/*from w w w. jav a 2 s.com*/ 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()); }