Job Parameters - Spring Batch Part 4

If you try rerunning your Spring Batch application from Part 3 blogpost, you will see that you are not able to do it. Why? Spring Batch utilizes the Job Repositories table and knows that the job has run already.

You have a Corporate Gift Shop., you want to continuously run the batch job so it picks up the orders placed and packages them and ships them. How do you rerun job?

The answer is simple, create different Jobs each time you want to run them by passing Job Parameters.


In intellij this is how I do, by editing run time parameters:

Rerun, and you should be able to see the output. Check the db tables, and you should see new entries.

Its very important that you are able to read the Job Parameters as they may have some important information you want to use while running your Job or Logging purpose.

If you want to package your gifts in a specific gift wrapper, you could specify like this:
@Bean
public Step packageStep() {
return this.stepBuilderFactory.get("packageStep").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
String giftWrapper = chunkContext.getStepContext().getJobParameters().get("giftWrapper").toString();
System.out.println("Packaging the gift with " + giftWrapper);
return RepeatStatus.FINISHED;
}
}).build();
}
Your code will look for a Job Property of giftWrapper using the Chunk Context and make it available for your step to use.

Lets run with "giftWrapper=Golden Box" as our Job Parameter.

2020-07-18 00:03:01.072  INFO 6488 --- [           main] c.r.s.SpringbootdemoApplication          : Started SpringbootdemoApplication in 3.444 seconds (JVM running for 4.251)
2020-07-18 00:03:01.080  INFO 6488 --- [           main] o.s.b.a.b.JobLauncherApplicationRunner   : Running default command line with: [giftWrapper=Golden Box]
2020-07-18 00:03:01.214  INFO 6488 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=giftShopJob]] launched with the following parameters: [{giftWrapper=Golden Box}]
2020-07-18 00:03:01.280  INFO 6488 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [readOrderStep]
Order Received
2020-07-18 00:03:01.314  INFO 6488 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [readOrderStep] executed in 34ms
2020-07-18 00:03:01.329  INFO 6488 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [packageStep]
Packaging the gift with Golden Box
2020-07-18 00:03:01.345  INFO 6488 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [packageStep] executed in 16ms
2020-07-18 00:03:01.364  INFO 6488 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [deliveryStep]
Package out for delivery
2020-07-18 00:03:01.379  INFO 6488 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [deliveryStep] executed in 15ms
2020-07-18 00:03:01.388  INFO 6488 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=giftShopJob]] completed with the following parameters: [{giftWrapper=Golden Box}] and the following status: [COMPLETED] in 126ms
2020-07-18 00:03:01.396  INFO 6488 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-07-18 00:03:01.415  INFO 6488 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.


You can also package your application into a jar and run it as follows:
java -jar <jarname>.jar "jobParameter=value"

Tip: You could use current date to make your Jobs Unique!

You can check the parameters passed to your Job by looking at the table BATCH_JOB_EXECUTION_PARAMS



Comments

Popular posts from this blog

Writing your own ejabberd Module

npm ECONNREFUSED error

Conditional Flow - Spring Batch Part 6