TaskExecutorRepeatTemplate repeatTemplate = new TaskExecutorRepeatTemplate(); repeatTemplate.setTaskExecutor(taskExecutor); repeatTemplate.setThrottleLimit(throttleLimit); stepOperations = repeatTemplate;
result = result && canContinue(status); executeAfterInterceptors(future.getContext(), status);
update(context); } while (queue.isEmpty() && !isComplete(context));
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 testSetThrottleLimit() throws Exception { try { new TaskExecutorRepeatTemplate().setThrottleLimit(-1); } catch (Exception e) { // unexpected - no check for illegal values fail("Unexpected Exception setting throttle limit"); } }
@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); }
TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate(); SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); taskExecutor.setConcurrencyLimit(300); template.setTaskExecutor(taskExecutor); template.setThrottleLimit(throttleLimit); template.iterate(callback);
public void testAsynchronousOnErrorInterceptorsPrecedence() throws Exception { TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate(); template.setTaskExecutor(new SimpleAsyncTaskExecutor()); final List<Object> calls = new ArrayList<>(); final List<Object> fails = new ArrayList<>(); template.setListeners(new RepeatListener[] { new RepeatListenerSupport() { @Override public void after(RepeatContext context, RepeatStatus result) { template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception {
@Test public void testThrottleLimitEarlyFinishOneThread() throws Exception { early = 4; SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); taskExecutor.setConcurrencyLimit(1); // This is kind of slow with only one thread, so reduce size: throttleLimit = 10; total = 20; template.setThrottleLimit(throttleLimit); template.setTaskExecutor(taskExecutor); template.iterate(callback); int frequency = Collections.frequency(items, "null"); // System.err.println("Frequency: " + frequency); // System.err.println("Items: " + items); assertEquals(total, items.size() - frequency); assertTrue(frequency <= throttleLimit + 1); }
TaskExecutorRepeatTemplate jobTemplate = new TaskExecutorRepeatTemplate(); final RepeatTemplate stepTemplate = new RepeatTemplate(); SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); taskExecutor.setConcurrencyLimit(2); jobTemplate.setTaskExecutor(taskExecutor); jobTemplate.iterate(jobCallback);
@Override public RepeatTemplate getRepeatTemplate() { return new TaskExecutorRepeatTemplate(); }
@Test public void testThrottleLimitEarlyFinishThreadStarvation() throws Exception { early = 2; ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); // Set the concurrency limit below the throttle limit for possible // starvation condition taskExecutor.setMaxPoolSize(20); taskExecutor.setCorePoolSize(10); taskExecutor.setQueueCapacity(0); // This is the most sensible setting, otherwise the bookkeeping in // ResultHolderResultQueue gets out of whack when tasks are aborted. taskExecutor .setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); taskExecutor.afterPropertiesSet(); template.setTaskExecutor(taskExecutor); template.iterate(callback); int frequency = Collections.frequency(items, "null"); // System.err.println("Frequency: " + frequency); // System.err.println("Items: " + items); // Extra tasks will be submitted before the termination is detected assertEquals(total, items.size() - frequency); assertTrue(frequency <= throttleLimit + 1); taskExecutor.destroy(); }
@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 testThrottleLimitEarlyFinish() throws Exception { early = 2; template.iterate(callback); int frequency = Collections.frequency(items, "null"); // System.err.println("Frequency: " + frequency); // System.err.println("Items: " + items); assertEquals(total, items.size() - frequency); assertTrue(frequency > 1); assertTrue(frequency <= throttleLimit + 1); }
@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); }
/** * 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); }
@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 testThrottleLimit() throws Exception { template.iterate(callback); int frequency = Collections.frequency(items, "null"); // System.err.println(items); // System.err.println("Frequency: " + frequency); assertEquals(total, items.size() - frequency); assertTrue(frequency > 1); assertTrue(frequency <= throttleLimit + 1); }
@Before public void setUp() { template = new TaskExecutorRepeatTemplate(); TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); threadPool.setMaxPoolSize(300); threadPool.afterPropertiesSet(); taskExecutor = threadPool; template.setTaskExecutor(taskExecutor); template.setThrottleLimit(throttleLimit);
result = result && canContinue(status); executeAfterInterceptors(future.getContext(), status);