@Test public void cannotExecuteWhenConcurrencyIsSwitchedOff() throws Exception { SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(); executor.setConcurrencyLimit(ConcurrencyThrottleSupport.NO_CONCURRENCY); assertTrue(executor.isThrottleActive()); exception.expect(IllegalStateException.class); executor.execute(new NoOpRunnable()); }
final RepeatTemplate stepTemplate = new RepeatTemplate(); SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); taskExecutor.setConcurrencyLimit(2); jobTemplate.setTaskExecutor(taskExecutor);
taskExecutor.setConcurrencyLimit(300); template.setTaskExecutor(taskExecutor); template.setThrottleLimit(throttleLimit);
@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); }
@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); }
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++); } }); }
// helper method to create a split flow out of a List of steps private static Flow createParallelFlow(List<Step> steps) { SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); // -1 indicates no concurrency limit at all, steps.size is in this case 2 threads, 1 means just 1 thread. taskExecutor.setConcurrencyLimit(steps.size()); List<Flow> flows = steps.stream() // we have to convert the steps to a flows .map(step -> // new FlowBuilder<Flow>("flow_" + step.getName()) // .start(step) // .build()) // .collect(Collectors.toList()); return new FlowBuilder<SimpleFlow>("parallelStepsFlow").split(taskExecutor) // .add(flows.toArray(new Flow[flows.size()])) // .build(); }
taskExecutor.setConcurrencyLimit(steps.size());
taskExecutor.setConcurrencyLimit(parallelCount);
asyncTaskExecutor.setConcurrencyLimit( 5 );