Search code examples
javaspringspring-batch

Spring Batch : async with AsyncItemProcessor and AsyncItemWriter


I want to improve my spring batch performance with async processing

I have noticed the class AsyncItemProcessor has a method setTaskExecutor :

public AsyncItemProcessor asyncDataProcessor() throws Exception {
    AsyncItemProcessor<List<Data>, List<Data>> processor = new AsyncItemProcessor<>();
    processor.setDelegate(dataProcessor());
    processor.setTaskExecutor(taskExecutor());
    return processor;
}

But StepBuilder class also has this taskExecutor method :

@Bean
public Step dataStep(JobRepository jobRepository,
                       PlatformTransactionManager transactionManager, DataService dataService) throws Exception {
    return new StepBuilder("dataStep", jobRepository)
            .<List<Data>, List<Data>>chunk(1, transactionManager)
            .reader(dataItemReader(dataService))
            .processor(asyncDataProcessor())
            .writer(asyncDataWriter(dataService))
            .taskExecutor(taskExecutor())
            .build();
}

Should I use this methods in both beans definitions ? What's the difference between the setTaskExecutor in AsyncItemProcessor and taskExecutor in StepBuilder ?


Solution

  • When you set a task executor on the step builder, you configure a multi-threaded step where chunks are processed concurrently by multiple threads (ie each chunk will be read/processed/written by a different thread). This is explained in the Multi-threaded Step section.

    With the AsyncItemProcessor/AsyncItemWriter approach, only the item processor is executed concurrently. See Asynchronous Processors