@Test public void testBeforeStepFailure() throws Exception { final RuntimeException exception = new RuntimeException(); taskletStep.setStepExecutionListeners(new StepExecutionListenerSupport[] { new StepExecutionListenerSupport() { @Override public void beforeStep(StepExecution stepExecution) { throw exception; } } }); taskletStep.execute(stepExecution); assertEquals(FAILED, stepExecution.getStatus()); assertTrue(stepExecution.getFailureExceptions().contains(exception)); assertEquals(2, jobRepository.getUpdateCount()); }
@Test /* * Exception in afterStep is ignored (only logged). */ public void testAfterStepFailureWhenTaskletFails() throws Exception { final RuntimeException exception = new RuntimeException(); taskletStep.setStepExecutionListeners(new StepExecutionListenerSupport[] { new StepExecutionListenerSupport() { @Override public ExitStatus afterStep(StepExecution stepExecution) { throw exception; } } }); taskletStep.execute(stepExecution); assertEquals(FAILED, stepExecution.getStatus()); assertTrue(stepExecution.getFailureExceptions().contains(taskletException)); assertFalse(stepExecution.getFailureExceptions().contains(exception)); assertEquals(2, jobRepository.getUpdateCount()); }
@Test public void testAfterStepFailureWhenTaskletSucceeds() throws Exception { final RuntimeException exception = new RuntimeException(); taskletStep.setStepExecutionListeners(new StepExecutionListenerSupport[] { new StepExecutionListenerSupport() { @Override public ExitStatus afterStep(StepExecution stepExecution) { throw exception; } } }); taskletStep.setTasklet(new Tasklet() { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext attributes) throws Exception { return RepeatStatus.FINISHED; } }); taskletStep.execute(stepExecution); assertEquals(COMPLETED, stepExecution.getStatus()); assertFalse(stepExecution.getFailureExceptions().contains(exception)); assertEquals(3, jobRepository.getUpdateCount()); }
@Test public void testInterrupted() throws Exception { taskletStep.setStepExecutionListeners(new StepExecutionListener[] { new InterruptionListener() }); taskletStep.execute(stepExecution); assertEquals(STOPPED, stepExecution.getStatus()); assertEquals(STOPPED.toString(), stepExecution.getExitStatus().getExitCode()); }
/** * Exception in {@link StepExecutionListener#afterStep(StepExecution)} * doesn't cause step failure. * @throws JobInterruptedException */ @Test public void testStepFailureInAfterStepCallback() throws JobInterruptedException { StepExecutionListener listener = new StepExecutionListenerSupport() { @Override public ExitStatus afterStep(StepExecution stepExecution) { throw new RuntimeException("exception thrown in afterStep to signal failure"); } }; step.setStepExecutionListeners(new StepExecutionListener[] { listener }); StepExecution stepExecution = new StepExecution(step.getName(), new JobExecution(jobInstance, jobParameters)); step.execute(stepExecution); assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); }
@Test public void testAfterStep() throws Exception { final ExitStatus customStatus = new ExitStatus("COMPLETED_CUSTOM"); step.setStepExecutionListeners(new StepExecutionListener[] { new StepExecutionListenerSupport() { @Override public ExitStatus afterStep(StepExecution stepExecution) { list.add("afterStepCalled"); return customStatus; } } }); RepeatTemplate stepTemplate = new RepeatTemplate(); stepTemplate.setCompletionPolicy(new SimpleCompletionPolicy(5)); step.setStepOperations(stepTemplate); JobExecution jobExecution = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); step.execute(stepExecution); assertEquals(1, list.size()); ExitStatus returnedStatus = stepExecution.getExitStatus(); assertEquals(customStatus.getExitCode(), returnedStatus.getExitCode()); assertEquals(customStatus.getExitDescription(), returnedStatus.getExitDescription()); }
/** * Register listeners with step and chunk. */ private void registerStepListeners(TaskletStep step, RepeatOperations chunkOperations) { for (Object itemHandler : new Object[] { getItemReader(), itemWriter, itemProcessor }) { if (StepListenerFactoryBean.isListener(itemHandler)) { StepListener listener = StepListenerFactoryBean.getListener(itemHandler); if (listener instanceof StepExecutionListener) { step.registerStepExecutionListener((StepExecutionListener) listener); } if (listener instanceof ChunkListener) { step.registerChunkListener((ChunkListener) listener); } } } step.setStepExecutionListeners(BatchListenerFactoryHelper.getListeners(listeners, StepExecutionListener.class) .toArray(new StepExecutionListener[] {})); step.setChunkListeners(BatchListenerFactoryHelper.getListeners(listeners, ChunkListener.class).toArray( new ChunkListener[] {})); }