Parallel Flow - Spring Batch Part 10

There will be cases when you will need to have parallel flows. For instance, in our case, Once the order is placed, we want to start the packaging but also we want to update our Gift Repository.

Lets see how it is done.

With this understanding, lets create three new steps.
  1. checkGiftRepositoryStep - To check if the repository is short of gifts 
  2. orderNewGiftsStep - Order new gifts
  3. giftRepositoryUpdatedStep - Update repository with new gifts 

Lets consider our repository is short of gifts and create the steps:

@Bean
public Step checkGiftRepositoryStep(){
return this.stepBuilderFactory.get("updateGiftRepositoryStep").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Checking if Gift Repository is short of gifts");
System.out.println("Gift Repository is short of gifts");
return RepeatStatus.FINISHED;
}
}).build();
}

@Bean
public Step orderNewGiftsStep(){
return this.stepBuilderFactory.get("orderNewGiftsStep").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Ordering new gifts");
return RepeatStatus.FINISHED;
}
}).build();
}

@Bean
public Step giftRepositoryUpdatedStep(){
return this.stepBuilderFactory.get("giftRepositoryUpdatedStep").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Gift Repository is full");
return RepeatStatus.FINISHED;
}
}).build();
}

Lets create the a Flow to call these steps:

@Bean
public Flow updateGiftRepositoryFlow(){
return new FlowBuilder<SimpleFlow>("updateGiftRepositoryFlow")
.start(checkGiftRepositoryStep())
.next(orderNewGiftsStep())
.next(giftRepositoryUpdatedStep())
.build();
}

We want the updateGiftRepositoryFlow and packagingAndDeliveryFlow to work at the same time. We can achieve it using SimpleAsyncTaskExecutor , lets see how to do it:

@Bean
public Job packageJob(){
return this.jobBuilderFactory.get("giftShopJob").start(readOrderStep())
.split(new SimpleAsyncTaskExecutor())
.add(packagingAndDeliveryFlow(), updateGiftRepositoryFlow())
.end()
.build();
}
 Lets run and check the output:

2020-08-07 00:41:00.786  INFO 14808 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=giftShopJob]] launched with the following parameters: [{chocolateType=DARK}]
2020-08-07 00:41:00.875  INFO 14808 --- [cTaskExecutor-3] o.s.batch.core.job.SimpleStepHandler     : Executing step: [readOrderStep]
2020-08-07 00:41:00.876  INFO 14808 --- [cTaskExecutor-2] o.s.batch.core.job.SimpleStepHandler     : Executing step: [updateGiftRepositoryStep]
2020-08-07 00:41:00.878  INFO 14808 --- [cTaskExecutor-1] o.s.batch.core.job.SimpleStepHandler     : Executing step: [packageStep]
Order Received
Packaging the gift
Checking if Gift Repository is short of gifts
Gift Repository is short of gifts
2020-08-07 00:41:00.918  INFO 14808 --- [cTaskExecutor-2] o.s.batch.core.step.AbstractStep         : Step: [updateGiftRepositoryStep] executed in 40ms
2020-08-07 00:41:00.918  INFO 14808 --- [cTaskExecutor-3] o.s.batch.core.step.AbstractStep         : Step: [readOrderStep] executed in 42ms
2020-08-07 00:41:00.918  INFO 14808 --- [cTaskExecutor-1] o.s.batch.core.step.AbstractStep         : Step: [packageStep] executed in 39ms
Customer choose: DELIVER
2020-08-07 00:41:00.939  INFO 14808 --- [cTaskExecutor-2] o.s.batch.core.job.SimpleStepHandler     : Executing step: [orderNewGiftsStep]
2020-08-07 00:41:00.939  INFO 14808 --- [cTaskExecutor-1] o.s.batch.core.job.SimpleStepHandler     : Executing step: [deliveryStep]
Ordering new gifts
Package out for delivery
2020-08-07 00:41:00.951  INFO 14808 --- [cTaskExecutor-2] o.s.batch.core.step.AbstractStep         : Step: [orderNewGiftsStep] executed in 12ms
2020-08-07 00:41:00.951  INFO 14808 --- [cTaskExecutor-1] o.s.batch.core.step.AbstractStep         : Step: [deliveryStep] executed in 12ms
2020-08-07 00:41:00.970  INFO 14808 --- [cTaskExecutor-2] o.s.batch.core.job.SimpleStepHandler     : Executing step: [giftRepositoryUpdatedStep]
Gift Repository is full
2020-08-07 00:41:00.981  INFO 14808 --- [cTaskExecutor-2] o.s.batch.core.step.AbstractStep         : Step: [giftRepositoryUpdatedStep] executed in 10ms
2020-08-07 00:41:00.989  INFO 14808 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=giftShopJob]] completed with the following parameters: [{chocolateType=DARK}] and the following status: [COMPLETED] in 157ms


As you can see the Packaging+Delivery and Refilling the Repository flows are executed in parallel.


Comments

Popular posts from this blog

Writing your own ejabberd Module

npm ECONNREFUSED error

Conditional Flow - Spring Batch Part 6