/** * Register each of the streams for callbacks at the appropriate time in the * step. The {@link ItemReader} and {@link ItemWriter} are automatically * registered, but it doesn't hurt to also register them here. Injected * dependencies of the reader and writer are not automatically registered, * so if you implement {@link ItemWriter} using delegation to another object * which itself is a {@link ItemStream}, you need to register the delegate * here. * * @param streams an array of {@link ItemStream} objects. */ public void setStreams(ItemStream[] streams) { for (int i = 0; i < streams.length; i++) { registerStream(streams[i]); } }
/** * Execution context must not be left empty even if job failed before * committing first chunk - otherwise ItemStreams won't recognize it is * restart scenario on next run. */ @Test public void testRestartAfterFailureInFirstChunk() throws Exception { MockRestartableItemReader reader = new MockRestartableItemReader() { @Override public String read() throws RuntimeException { // fail on the very first item throw new RuntimeException("CRASH!"); } }; step.setTasklet(new TestingChunkOrientedTasklet<>(reader, itemWriter)); step.registerStream(reader); StepExecution stepExecution = new StepExecution(step.getName(), new JobExecution(jobInstance, jobParameters)); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); Throwable expected = stepExecution.getFailureExceptions().get(0); assertEquals("CRASH!", expected.getMessage()); assertFalse(stepExecution.getExecutionContext().isEmpty()); assertTrue(stepExecution.getExecutionContext().getString("spam").equals("bucket")); }
@Test public void testStatusForCloseFailedException() throws Exception { MockRestartableItemReader itemReader = new MockRestartableItemReader() { @Override public void close() throws ItemStreamException { super.close(); // Simulate failure on rollback when stream resets throw new RuntimeException("Bar"); } }; step.setTasklet(new TestingChunkOrientedTasklet<>(itemReader, itemWriter)); step.registerStream(itemReader); JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); stepExecution.setExecutionContext(foobarEc); // step.setLastExecution(stepExecution); step.execute(stepExecution); // The job actually completed, but the streams couldn't be closed. assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); String msg = stepExecution.getExitStatus().getExitDescription(); assertEquals("", msg); Throwable ex = stepExecution.getFailureExceptions().get(0); // The original rollback was caused by this one: assertEquals("Bar", ex.getMessage()); }
@Test public void testNonRestartedJob() throws Exception { MockRestartableItemReader tasklet = new MockRestartableItemReader(); step.setTasklet(new TestingChunkOrientedTasklet<>(tasklet, itemWriter)); step.registerStream(tasklet); JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); step.execute(stepExecution); assertFalse(tasklet.isRestoreFromCalled()); assertTrue(tasklet.isGetExecutionAttributesCalled()); }
@Test public void testStreamManager() throws Exception { MockRestartableItemReader reader = new MockRestartableItemReader() { @Override public String read() { return "foo"; } @Override public void update(ExecutionContext executionContext) { super.update(executionContext); executionContext.putString("foo", "bar"); } }; step.setTasklet(new TestingChunkOrientedTasklet<>(reader, itemWriter)); step.registerStream(reader); JobExecution jobExecution = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); assertEquals(false, stepExecution.getExecutionContext().containsKey("foo")); step.execute(stepExecution); // At least once in that process the statistics service was asked for // statistics... assertEquals("bar", stepExecution.getExecutionContext().getString("foo")); }
private void setUp() throws Exception { step = new TaskletStep("stepName"); ResourcelessTransactionManager transactionManager = new ResourcelessTransactionManager(); step.setTransactionManager(transactionManager); RepeatTemplate chunkTemplate = new RepeatTemplate(); chunkTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2)); step.setTasklet(new TestingChunkOrientedTasklet<>(new ListItemReader<>(items), itemProcessor, itemWriter, chunkTemplate)); jobRepository = new JobRepositorySupport(); step.setJobRepository(jobRepository); TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate(); template.setThrottleLimit(throttleLimit); SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); taskExecutor.setConcurrencyLimit(concurrencyLimit); template.setTaskExecutor(taskExecutor); step.setStepOperations(template); step.registerStream(new ItemStreamSupport() { private int count = 0; @Override public void update(ExecutionContext executionContext) { super.update(executionContext); executionContext.putInt("counter", count++); } }); }
/** * Register each of the streams for callbacks at the appropriate time in the * step. The {@link ItemReader} and {@link ItemWriter} are automatically * registered, but it doesn't hurt to also register them here. Injected * dependencies of the reader and writer are not automatically registered, * so if you implement {@link ItemWriter} using delegation to another object * which itself is a {@link ItemStream}, you need to register the delegate * here. * * @param streams an array of {@link ItemStream} objects. */ public void setStreams(ItemStream[] streams) { for (int i = 0; i < streams.length; i++) { registerStream(streams[i]); } }
/** * Register each of the streams for callbacks at the appropriate time in the * step. The {@link ItemReader} and {@link ItemWriter} are automatically * registered, but it doesn't hurt to also register them here. Injected * dependencies of the reader and writer are not automatically registered, * so if you implement {@link ItemWriter} using delegation to another object * which itself is a {@link ItemStream}, you need to register the delegate * here. * * @param streams an array of {@link ItemStream} objects. */ public void setStreams(ItemStream[] streams) { for (int i = 0; i < streams.length; i++) { registerStream(streams[i]); } }
/** * Register each of the streams for callbacks at the appropriate time in the * step. The {@link ItemReader} and {@link ItemWriter} are automatically * registered, but it doesn't hurt to also register them here. Injected * dependencies of the reader and writer are not automatically registered, * so if you implement {@link ItemWriter} using delegation to another object * which itself is a {@link ItemStream}, you need to register the delegate * here. * * @param streams an array of {@link ItemStream} objects. */ public void setStreams(ItemStream[] streams) { for (int i = 0; i < streams.length; i++) { registerStream(streams[i]); } }
/** * Register each of the streams for callbacks at the appropriate time in the * step. The {@link ItemReader} and {@link ItemWriter} are automatically * registered, but it doesn't hurt to also register them here. Injected * dependencies of the reader and writer are not automatically registered, * so if you implement {@link ItemWriter} using delegation to another object * which itself is a {@link ItemStream}, you need to register the delegate * here. * * @param streams an array of {@link ItemStream} objects. */ public void setStreams(ItemStream[] streams) { for (int i = 0; i < streams.length; i++) { registerStream(streams[i]); } }
step.registerStream(stream); step.registerStream(chunkMonitor); boolean concurrent = taskExecutor != null && !(taskExecutor instanceof SyncTaskExecutor); if (!concurrent) {