Hello everyone,
Greetings today!
Let's get started with the Spring batch job listener today.
What exactly is a Spring Batch Job Listener?
- Job Listener is a method called before the job and after the job based on our implementation.
What is the Use Of a Job Listener?
- Job listener can be used to configure/pass some files run time to spring batch reader, or notify start/end of batch job to an external system, for example, sending email on starting of batch job execution, sending email on batch job fail, or logging errors when a batch job fails, and so on.
In the spring batch, we can configure job listeners in two ways.
- Adding a JobExecutionListener and overriding the beforeJob and afterJob methods
package com.order.processor.listener; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobExecutionListener; import org.springframework.stereotype.Component; @Component public class OrderJobExecutionListener implements JobExecutionListener { @Override public void beforeJob(JobExecution jobExecution) { System.out.println("Before Job Called...."); } @Override public void afterJob(JobExecution jobExecution) { System.out.println("After Job Called...."); } }
- Create a class and annotate methods with @BeforeJob and @AfterJob to be called before and after the batch job is executed.
package com.order.processor.listener; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.annotation.AfterJob; import org.springframework.batch.core.annotation.BeforeJob; import org.springframework.stereotype.Component; @Component public class OrderJobExecutionListenerV1 { @BeforeJob public void beforeJob(JobExecution jobExecution) { System.out.println("Before Job Called for V1...."); } @AfterJob public void afterJob(JobExecution jobExecution) { System.out.println("After Job Called for V2...."); } }
As shown below, we can also check the job status using the afterJob method.
@Override public void afterJob(JobExecution jobExecution) { if(ExitStatus.FAILED.getExitCode() .equals(jobExecution.getExitStatus().getExitCode())){ jobExecution.getAllFailureExceptions() .forEach(throwable -> throwable.printStackTrace()); } }
The listener must also be configured in the spring batch configuration class or it will not be called. As shown below, we can do so.
@Bean(name = "processOrderJob") public Job processOrderJob() { return jobBuilderFactory.get("generateReportCard"). incrementer(new RunIdIncrementer()). start(processOrder()). listener(orderJobExecutionListener). listener(orderJobExecutionListenerV1). build(); }
For your convenience, the complete BatchConfig.java code is provided below.
package com.order.processor.config; @Configuration public class BatchConfig { @Autowired private JobBuilderFactory jobBuilderFactory; @Autowired private StepBuilderFactory stepBuilderFactory; @Autowired private OrderReader orderReader; @Autowired private OrderWriter orderWriter; @Autowired private OrderJobExecutionListener orderJobExecutionListener; @Autowired private OrderJobExecutionListenerV1 orderJobExecutionListenerV1; @Bean(name = "processOrderJob") public Job processOrderJob() { return jobBuilderFactory .get("generateReportCard") .incrementer(new RunIdIncrementer()) .start(processOrder()) .listener(orderJobExecutionListener) .listener(orderJobExecutionListenerV1) .build(); } @Bean public Step processOrder() { return stepBuilderFactory.get("processOrder") .<OrderDTO, OrderDTO>chunk(1) .reader(orderReader) .writer(orderWriter) .build(); } }
So far, we've seen how to use and configure job listeners - beforeJob and afterJob - in the spring batch with spring boot.
Please leave a comment if you have any questions.
Thanks
Enjoy your learning!
Other reference articles
0 Comments
If you have any doubts let me know.