public RepeatTemplate getRepeatTemplate() { TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate(); template.setTaskExecutor(new SimpleAsyncTaskExecutor()); // Set default completion above number of items in input file template.setCompletionPolicy(new SimpleCompletionPolicy(8)); return template; }
@Test public void testThrottleLimitWithEarlyCompletion() throws Exception { early = 2; template.setCompletionPolicy(new SimpleCompletionPolicy(10)); template.iterate(callback); int frequency = Collections.frequency(items, "null"); assertEquals(10, items.size() - frequency); // System.err.println("Frequency: " + frequency); assertEquals(0, frequency); }
@Test public void testErrorThrownByCallback() throws Exception { callback = new RepeatCallback() { private volatile AtomicInteger count = new AtomicInteger(0); @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { int position = count.incrementAndGet(); if(position == 4) { throw new OutOfMemoryError("Planned"); } else { return RepeatStatus.CONTINUABLE; } } }; template.setCompletionPolicy(new SimpleCompletionPolicy(10)); try { template.iterate(callback); fail("Expected planned exception"); } catch (OutOfMemoryError oome) { assertEquals("Planned", oome.getMessage()); } catch (Exception e) { e.printStackTrace(); fail("Wrong exception was thrown: " + e); } }
@Test public void testEarlyCompletionWithException() throws Exception { TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate(); SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); template.setCompletionPolicy(new SimpleCompletionPolicy(20)); taskExecutor.setConcurrencyLimit(2); template.setTaskExecutor(taskExecutor); try { template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; throw new IllegalStateException("foo!"); } }); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { assertEquals("foo!", e.getMessage()); } assertTrue("Too few attempts: " + count, count >= 1); assertTrue("Too many attempts: " + count, count <= 10); }
@Test public void testExceptionHandlerSwallowsException() throws Exception { TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate(); SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); template.setCompletionPolicy(new SimpleCompletionPolicy(4)); taskExecutor.setConcurrencyLimit(2); template.setTaskExecutor(taskExecutor); template.setExceptionHandler(new ExceptionHandler() { @Override public void handleException(RepeatContext context, Throwable throwable) throws Throwable { count++; } }); template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { throw new IllegalStateException("foo!"); } }); assertTrue("Too few attempts: " + count, count >= 1); assertTrue("Too many attempts: " + count, count <= 10); }
/** * 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); }