/** * Public setter for the {@link Tasklet}. * * @param tasklet the {@link Tasklet} to set */ public void setTasklet(Tasklet tasklet) { this.tasklet = tasklet; if (tasklet instanceof StepExecutionListener) { registerStepExecutionListener((StepExecutionListener) tasklet); } }
step.registerStepExecutionListener((StepExecutionListener) chunkWriter);
@Test public void testDirectlyInjectedListener() throws Exception { step.registerStepExecutionListener(new StepExecutionListenerSupport() { @Override public void beforeStep(StepExecution stepExecution) { list.add("foo"); } @Override public ExitStatus afterStep(StepExecution stepExecution) { list.add("bar"); return null; } }); JobExecution jobExecution = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); step.execute(stepExecution); assertEquals(2, list.size()); }
@Test public void testDirectlyInjectedListenerOnError() throws Exception { step.registerStepExecutionListener(new StepExecutionListenerSupport() { @Override public ExitStatus afterStep(StepExecution stepExecution) { list.add("exception"); return null; } }); step.setTasklet(new TestingChunkOrientedTasklet<>(new MockRestartableItemReader() { @Override public String read() throws RuntimeException { throw new RuntimeException("FOO"); } }, itemWriter)); JobExecution jobExecution = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); step.execute(stepExecution); assertEquals("FOO", stepExecution.getFailureExceptions().get(0).getMessage()); assertEquals(1, list.size()); }
@Test public void testListenerCalledBeforeStreamOpened() throws Exception { MockRestartableItemReader reader = new MockRestartableItemReader() { @Override public void beforeStep(StepExecution stepExecution) { list.add("foo"); } @Override public void open(ExecutionContext executionContext) throws ItemStreamException { super.open(executionContext); assertEquals(1, list.size()); } }; step.setStreams(new ItemStream[] { reader }); step.registerStepExecutionListener(reader); StepExecution stepExecution = new StepExecution(step.getName(), new JobExecution(jobInstance, jobParameters)); step.execute(stepExecution); assertEquals(1, list.size()); }
@Test public void testExitCodeCustomClassification() throws Exception { ItemReader<String> itemReader = new ItemReader<String>() { @Override public String read() throws Exception { throw new RuntimeException(); } }; step.setTasklet(new TestingChunkOrientedTasklet<>(itemReader, itemWriter)); step.registerStepExecutionListener(new StepExecutionListenerSupport() { @Override public ExitStatus afterStep(StepExecution stepExecution) { return ExitStatus.FAILED.addExitDescription("FOO"); } }); JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); try { step.execute(stepExecution); } catch (Exception ex) { ExitStatus status = stepExecution.getExitStatus(); assertEquals(ExitStatus.FAILED.getExitCode(), status.getExitCode()); String description = status.getExitDescription(); assertTrue("Description does not include 'FOO': " + description, description.indexOf("FOO") >= 0); } }
/** * Public setter for the {@link Tasklet}. * * @param tasklet the {@link Tasklet} to set */ public void setTasklet(Tasklet tasklet) { this.tasklet = tasklet; if (tasklet instanceof StepExecutionListener) { registerStepExecutionListener((StepExecutionListener) tasklet); } }
/** * Public setter for the {@link Tasklet}. * * @param tasklet the {@link Tasklet} to set */ public void setTasklet(Tasklet tasklet) { this.tasklet = tasklet; if (tasklet instanceof StepExecutionListener) { registerStepExecutionListener((StepExecutionListener) tasklet); } }
/** * Public setter for the {@link Tasklet}. * * @param tasklet the {@link Tasklet} to set */ public void setTasklet(Tasklet tasklet) { this.tasklet = tasklet; if (tasklet instanceof StepExecutionListener) { registerStepExecutionListener((StepExecutionListener) tasklet); } }
/** * Public setter for the {@link Tasklet}. * * @param tasklet the {@link Tasklet} to set */ public void setTasklet(Tasklet tasklet) { this.tasklet = tasklet; if (tasklet instanceof StepExecutionListener) { registerStepExecutionListener((StepExecutionListener) tasklet); } }
/** * 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[] {})); }