The tutorial explains how we can use concurrent steps in Spring Batch.
With Spring Batch, you can define and run jobs. The jobs may have a step or many steps.
Typically, Batch Jobs are long-running, non-interactive and process large volumes of data, more than fits in memory or a single transaction. During a step, the job do something, a particular task, named tasklet. When you define a step you can define the next step you have to execute. A job could execute steps in a defined order.
We can have in Spring Batch: sequential steps and parallel steps. We can execute some tasks (steps) when a condition is true or false. Because of these things, you can create workflows using Spring Batch.
When you define a step you have 2 models:
run a simple task (tasklet)
run a step using the following pattern READ-PROCESS-WRITE
This tutorial explains to you how to use the first approach.
In order to create a simple example with 2 or more concurrent steps, first you have to create a simple Maven Spring project with the pom.xml file having the following dependencies:
At this point it is supposed that the Job Repository has been configured already.
In order to see how to create a tasklet step, you have to create a batch.properties file with the following content :
packagecom.examples.tasklets;importorg.springframework.batch.core.StepContribution;importorg.springframework.batch.core.scope.context.ChunkContext;importorg.springframework.batch.core.step.tasklet.Tasklet;importorg.springframework.batch.repeat.RepeatStatus;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassMyTasklet0implementsTasklet{@OverridepublicRepeatStatusexecute(StepContribution contribution,ChunkContext chunkContext)throwsException{System.out.println("MyTasklet#0 is started ...");Thread.sleep(2000);System.out.println("MyTasklet#0 is completed ...");returnRepeatStatus.FINISHED;}}
packagecom.examples.tasklets;importorg.springframework.batch.core.StepContribution;importorg.springframework.batch.core.scope.context.ChunkContext;importorg.springframework.batch.core.step.tasklet.Tasklet;importorg.springframework.batch.repeat.RepeatStatus;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassMyTasklet1implementsTasklet{@OverridepublicRepeatStatusexecute(StepContribution contribution,ChunkContext chunkContext)throwsException{System.out.println("MyTasklet#1 is started ...");Thread.sleep(2000);System.out.println("MyTasklet#1 is completed ...");returnRepeatStatus.FINISHED;}}
packagecom.examples.tasklets;importorg.springframework.batch.core.StepContribution;importorg.springframework.batch.core.scope.context.ChunkContext;importorg.springframework.batch.core.step.tasklet.Tasklet;importorg.springframework.batch.repeat.RepeatStatus;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassMyTasklet2implementsTasklet{@OverridepublicRepeatStatusexecute(StepContribution contribution,ChunkContext chunkContext)throwsException{System.out.println("MyTasklet#2 is started ...");Thread.sleep(2000);System.out.println("MyTasklet#2 is completed ...");returnRepeatStatus.FINISHED;}}