Reusability - Spring Batch Part 9

With SpringBatch, you are able to reuse existing steps, jobs.

Lets look at how we can reuse existing steps.

In our case, when a customer orders a chocolate box, we also want to package it and deliver it. These are the steps we used while the customer ordered a corporate gift as well. 




As you can see from the above diagram, after the Package Step everything is the same. (Steps highlighted in blue)

We can reuse these steps using Flows. We will create a Flow consisting of the common steps and use it in both our jobs.
@Bean
public Flow packagingAndDeliveryFlow(){
return new FlowBuilder<SimpleFlow>("packageAndDeliveryFlow").start(packageStep())
.on("FAILED").fail()
.from(packageStep())
.on("*").to(pickupOrDeliveryDecider())
.on("DELIVER").to(deliveryStep())
.from(pickupOrDeliveryDecider())
.on("PICKUP").to(pickupStep())
.build();
}
As you can see, the packageAndDeliveryFlow is replicating what we did for the packageJob. We will now have to change our packageJob and orderChocolateBoxJob to use this flow:

@Bean
public Job packageJob(){
return this.jobBuilderFactory.get("giftShopJob").start(readOrderStep())
.on("*").to(packagingAndDeliveryFlow())
.end()
.build();
}

@Bean
public Job orderChocolateBoxJob(){
return this.jobBuilderFactory.get("orderChocolateBoxJob")
.start(selectChocolatesStep())
.on("NUTTY_CHOCOLATES").to(addNoteStep()).next(arrangeChocolatesStep())
.from(selectChocolatesStep()).on("NORMAL_CHOCOLATES").to(arrangeChocolatesStep())
.next(packagingAndDeliveryFlow())
.end()
.build();
}

Lets run the application and see the output:
2020-08-05 00:52:48.906  INFO 19936 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=giftShopJob]] launched with the following parameters: [{chocolateType=DARK}]
2020-08-05 00:52:48.962  INFO 19936 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [readOrderStep]
Order Received
2020-08-05 00:52:48.985  INFO 19936 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [readOrderStep] executed in 22ms
2020-08-05 00:52:48.998  INFO 19936 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [packageStep]
Packaging the gift
2020-08-05 00:52:49.005  INFO 19936 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [packageStep] executed in 7ms
Customer choose: DELIVER
2020-08-05 00:52:49.017  INFO 19936 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [deliveryStep]
Package out for delivery
2020-08-05 00:52:49.025  INFO 19936 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [deliveryStep] executed in 8ms
2020-08-05 00:52:49.033  INFO 19936 --- [           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 94ms
2020-08-05 00:52:49.046  INFO 19936 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=orderChocolateBoxJob]] launched with the following parameters: [{chocolateType=DARK}]
2020-08-05 00:52:49.058  INFO 19936 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [selectChocolatesStep]
Before arranging chocolates
Ordering Chocolate : DARK
After arranging chocolates
2020-08-05 00:52:49.066  INFO 19936 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [selectChocolatesStep] executed in 7ms
2020-08-05 00:52:49.079  INFO 19936 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [arrangeChocolatesStep]
Ordering Chocolate : DARK
2020-08-05 00:52:49.086  INFO 19936 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [arrangeChocolatesStep] executed in 7ms
2020-08-05 00:52:49.098  INFO 19936 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [packageStep]
Packaging the gift
2020-08-05 00:52:49.105  INFO 19936 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [packageStep] executed in 7ms
Customer choose: PICKUP
2020-08-05 00:52:49.117  INFO 19936 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [pickupStep]
Package ready for pickup
2020-08-05 00:52:49.124  INFO 19936 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [pickupStep] executed in 7ms
2020-08-05 00:52:49.130  INFO 19936 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=orderChocolateBoxJob]] completed with the following parameters: [{chocolateType=DARK}] and the following status: [COMPLETED] in 81ms
2020-08-05 00:52:49.135  INFO 19936 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-08-05 00:52:49.146  INFO 19936 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.



As you can see we were able to reuse the steps using Flows in SpringBatch.



Note: I have dropped the Spring Job Repository tables just to able to rerun the application just to make it easy to rerun the Job

DROP TABLE BATCH_STEP_EXECUTION_CONTEXT ;
DROP TABLE BATCH_STEP_EXECUTION ;
DROP TABLE BATCH_JOB_INSTANCE ;
DROP TABLE BATCH_JOB_EXECUTION_PARAMS ;
DROP TABLE BATCH_JOB_EXECUTION_CONTEXT ;
DROP TABLE BATCH_JOB_EXECUTION ;

Comments

Popular posts from this blog

Writing your own ejabberd Module

npm ECONNREFUSED error

Conditional Flow - Spring Batch Part 6