Creating multiple SpringBatch Job - Spring Batch Part2
Let us consider, you own a corporate gift shop and its Christmas time!! You are accepting bulk orders and offer delivery. Lets see how to solve it using Spring Batch.
Entire sample code is available at: https://github.com/ricsr/spring-batch-demo/tree/exercise_02/src/main/java/com/ricsr/springbootdemo
There are three steps involved in this process:
- Read an order
- Package an order
- Ship the order for delivery
Let us create these three steps:
@Bean
public Step readOrderStep() {
return this.stepBuilderFactory.get("readOrderStep").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Order Received");
return RepeatStatus.FINISHED;
}
}).build();
}
@Bean
public Step packageStep() {
return this.stepBuilderFactory.get("packageStep").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Packaging the gift");
return RepeatStatus.FINISHED;
}
}).build();
}
@Bean
public Step deliveryStep() {
return this.stepBuilderFactory.get("deliveryStep").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Package out for delivery");
return RepeatStatus.FINISHED;
}
}).build();
}
In order for our application to run these steps in an order, we use the next method on the JobBuilderFactory
@Bean
public Job packageJob(){
return this.jobBuilderFactory.get("packageJob").start(readOrderStep())
.next(packageStep())
.next(deliveryStep()).build();
}
Lets run the application:
2020-07-16 23:23:28.681 INFO 23732 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=giftShopJob]] launched with the following parameters: [{}]
2020-07-16 23:23:28.742 INFO 23732 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [readOrderStep]
Order Received
2020-07-16 23:23:28.774 INFO 23732 --- [ main] o.s.batch.core.step.AbstractStep : Step: [readOrderStep] executed in 32ms
2020-07-16 23:23:28.787 INFO 23732 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [packageStep]
Packaging the gift
2020-07-16 23:23:28.796 INFO 23732 --- [ main] o.s.batch.core.step.AbstractStep : Step: [packageStep] executed in 9ms
2020-07-16 23:23:28.808 INFO 23732 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [deliveryStep]
Package out for delivery
2020-07-16 23:23:28.817 INFO 23732 --- [ main] o.s.batch.core.step.AbstractStep : Step: [deliveryStep] executed in 9ms
2020-07-16 23:23:28.837 INFO 23732 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=giftShopJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 106ms
2020-07-16 23:23:28.843 INFO 23732 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-07-16 23:23:28.861 INFO 23732 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
BUILD SUCCESSFUL in 4s
3 actionable tasks: 2 executed, 1 up-to-date
11:23:28 PM: Task execution finished 'SpringbootdemoApplication.main()'.
Entire sample code is available at: https://github.com/ricsr/spring-batch-demo/tree/exercise_02/src/main/java/com/ricsr/springbootdemo
Comments