Spring Batch Course Notes 13- Async Job


Synchronous execution will the previous task wait for the next task to run. In asynchronous executioner we can make the task run parallely instead of one task waiting on the other.

  • Need a POM Dependency of spring-batch-integration
  • Along with creating ItemProcessor we can create a new processor using the class AsyncItemProcessor.
    • Delegate the item processor object into the AsyncItemProcessor object using the the .delegate method.
    • Add TaskExecutor using the setTaskExecutor on the AsyncItemProcessor object. Setting it to new SimpleAsyncTaskExecutor should do the trick.
    • Set the AsyncItemProcessor into the job.
  • Create a new Object of the AsyncItemWriter class which taken in the data processed by the Async Item Processor and writes the data.
    • Delegate the custom ItemWriter object into the AsyncItemWriter object using the .delegate method.
    • Set the Task Executor as new SimpleAsyncTaskExecutor.
    • Add the AsyncItemWriter into the .writer method of the StepBuilder instead of the Custom Item Writer.

Difference between a Async Processing and multithreaded processing is that multithreaded processing processes the entire chunk in parallel due to this the order of records written will be random. But in case of Async Processing we run the processor and Writer parallely separately due this this the order of the output will not be impacted.

Leave a comment