@Test public void testExecute() throws Exception { template.iterate(new ItemReaderRepeatCallback<>(provider, processor)); assertEquals(NUMBER_OF_ITEMS, processor.count); }
/** * Check that a dedicated TerminationPolicy can terminate the batch. * * @throws Exception */ @Test public void testEarlyCompletionWithPolicy() throws Exception { template.setCompletionPolicy(new SimpleCompletionPolicy(2)); template.iterate(new ItemReaderRepeatCallback<>(provider, processor)); assertEquals(2, processor.count); }
/** * Test that a result is returned from the batch. * @throws Exception */ @Test public void testResult() throws Exception { RepeatStatus result = template.iterate(new ItemReaderRepeatCallback<>(provider, processor)); assertEquals(NUMBER_OF_ITEMS, processor.count); // We are complete - do not expect to be called again assertFalse(result.isContinuable()); }
/** * Chunking using a dedicated TerminationPolicy. Transactions would be laid * on at the level of chunkTemplate.execute() or the surrounding callback. */ @Test public void testChunkedBatchWithTerminationPolicy() throws Exception { RepeatTemplate repeatTemplate = new RepeatTemplate(); final RepeatCallback callback = new ItemReaderRepeatCallback<>(provider, processor); final RepeatTemplate chunkTemplate = new RepeatTemplate(); // The policy is resettable so we only have to resolve this dependency // once chunkTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2)); RepeatStatus result = repeatTemplate.iterate(new NestedRepeatCallback(chunkTemplate, callback) { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; // for test assertion return super.doInIteration(context); } }); assertEquals(NUMBER_OF_ITEMS, processor.count); // The chunk executes 3 times because the last one // returns false. We terminate the main batch when // we encounter a partially empty chunk. assertEquals(3, count); assertFalse(result.isContinuable()); }
/** * Chunking with an asynchronous taskExecutor in the chunks. Transactions * have to be at the level of the business callback. */ @Test public void testAsynchronousChunkedBatchWithCompletionPolicy() throws Exception { RepeatTemplate repeatTemplate = new RepeatTemplate(); final RepeatCallback callback = new ItemReaderRepeatCallback<>(provider, processor); final TaskExecutorRepeatTemplate chunkTemplate = new TaskExecutorRepeatTemplate(); // The policy is resettable so we only have to resolve this dependency // once chunkTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2)); chunkTemplate.setTaskExecutor(new SimpleAsyncTaskExecutor()); RepeatStatus result = repeatTemplate.iterate(new NestedRepeatCallback(chunkTemplate, callback) { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; // for test assertion return super.doInIteration(context); } }); assertEquals(NUMBER_OF_ITEMS, processor.count); assertFalse(result.isContinuable()); assertTrue("Expected at least 3 chunks but found: "+count, count>=3); }