Search code examples
springspring-batchbatch-processing

How to implement AsyncItemProcessor and AsyncItemWriter with Spring Batch 5?


I'm currently working on a Spring Batch implementation and encountered an issue while following a tutorial on YouTube titled "High Performance Batch Processing." The tutorial suggests using "AsyncItemProcessor and AsyncItemWriter" for scaling, but I've noticed that the classes AsyncItemProcessor and AsyncItemWriter no longer exist in the latest version of Spring Batch.

I've searched extensively for a solution, checking both the video and the migration guide, but haven't found any guidance on implementing asynchronous item processing and writing.

Could someone provide assistance on how to achieve Async Item processing and writing in Spring Batch?

Thank you in advance for your help!

Configuring the reader, processor and writer:

@Configuration
public class ItemConfig {

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @Bean
    public JpaPagingItemReader<Employee> itemReader() {
        JpaPagingItemReaderBuilder<Employee> reader = new JpaPagingItemReaderBuilder<>();
        reader.entityManagerFactory(entityManagerFactory);
        reader.pageSize(1000);
        reader.queryString("SELECT e FROM employees e");
        reader.name("itemReader");
        return reader.build();
    }

    @Bean
    public ItemProcessor<Employee, Employee> processor() {
        return new ItemProcessorImpl();
    }

    @Bean
    public ItemWriter<Employee> itemWriter() {
        final JpaItemWriter<Employee> writer = new JpaItemWriter<>();
        writer.setEntityManagerFactory(entityManagerFactory);
        return writer;
    }
}

Configuring the job and step:

@Configuration
public class BatchConfig {

    @Bean
    public Job job(JobRepository jobRepository, Step step) {
        return new JobBuilder("job", jobRepository)
                .start(step)
                .build();
    }

    @Bean
    public Step step(JobRepository jobRepository,
                     PlatformTransactionManager transactionManager,
                     JpaPagingItemReader<Employee> itemReader,
                     ItemProcessor<Employee, Employee> processor,
                     ItemWriter<Employee> itemWriter) {
        return new StepBuilder("step", jobRepository)
                .<Employee, Employee>chunk(1000, transactionManager)
                .reader(itemReader)
                .processor(processor)
                .writer(itemWriter)
                .build();
    }
}

Solution

  • The classes AsyncItemProcessor and AsyncItemWriter are contained in the jar spring-batch-integration. You need to add it to your project. If you use Spring Boot and Maven, then you can use the following snippet:

    <dependency>
      <groupId>org.springframework.batch</groupId>
      <artifactId>spring-batch-integration</artifactId>
    </dependency>
    

    By default, a Spring Boot app with Spring Batch uses only the jars for spring-batch-core, spring-batch-infrastructure, and for tests spring-batch-test.