In this post, we will talk about Spring Batch Listeners Examples, these Listeners are provided by the Spring Batch framework. Listeners are the entities that help to intercept the execution of a Job or a Step and allow us to perform some functionality. The post will give examples of Spring Batch Java configuration.
Spring Batch Listeners Examples
How to configure JobExecutionListener
Here is the JobExecutionListener configuration
1 2 3 4 5 6 7 |
@Bean public Job helloWorlJob(JobBuilderFactory jobBuilders, StepBuilderFactory stepBuilders) { return jobBuilders.get("myJob") .listener(new MyJobListener())//add your Listener here .start(helloWorldStep(stepBuilders)).build(); } |
Here is a simple implementation of MyJobListener
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobExecutionListener; public class MyJobListener implements JobExecutionListener { public void beforeJob(JobExecution jobExecution) { System.out.println("Before job starts"); } public void afterJob(JobExecution jobExecution) { System.out.println("After job ends."); } } |
How to configure StepExecutionListener
Here is the StepExecutionListener configuration
1 2 3 4 5 6 7 |
@Bean public Step helloWorldStep(StepBuilderFactory stepBuilders) { return stepBuilders.get("myStep") .listener(new MyStepListener())//add your Listener here .<Employee, String>chunk(10).reader(reader()) .processor(processor()).writer(writer()).build(); } |
Here is a simple implementation of MyStepListener
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.javabycode.listener; import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.StepExecutionListener; public class MyStepListener implements StepExecutionListener { @Override public void beforeStep(StepExecution stepExecution) { System.out.println("Before step starts"); } @Override public ExitStatus afterStep(StepExecution stepExecution) { System.out.println("After step ends."); return ExitStatus.COMPLETED; } } |
How to configure ItemReadListener
Here is the ItemReadListener configuration
1 2 3 4 5 6 7 |
@Bean public Step helloWorldStep(StepBuilderFactory stepBuilders) { return stepBuilders.get("myStep") .listener(new MyStepItemReadListener())//add your Listener here .<Employee, String>chunk(10).reader(reader()) .processor(processor()).writer(writer()).build(); } |
Here is a simple implementation of MyStepItemReadListener
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.springframework.batch.core.ItemReadListener; public class MyStepItemReadListener implements ItemReadListener<String> { @Override public void beforeRead() { System.out.println("Before read starts"); } @Override public void afterRead(String item) { System.out.println("After read ends"); } @Override public void onReadError(Exception ex) { System.out.println("Exception on reading"); } } |
How to configure ItemProcessListener
Here is the ItemProcessListener configuration
1 2 3 4 5 6 7 |
@Bean public Step helloWorldStep(StepBuilderFactory stepBuilders) { return stepBuilders.get("myStep") .listener(new MyStepItemProcessListener())//add your Listener here .<Employee, String>chunk(10).reader(reader()) .processor(processor()).writer(writer()).build(); } |
Here is a simple implementation of MyStepItemProcessListener
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.springframework.batch.core.ItemProcessListener; public class MyStepItemProcessListener implements ItemProcessListener<String, Number> { @Override public void beforeProcess(String item) { System.out.println("Before process starts"); } @Override public void afterProcess(String item, Number result) { System.out.println("After process ends"); } @Override public void onProcessError(String item, Exception e) { System.out.println("Exception on processing"); } } |
How to configure ItemWriteListener
Here is the ItemWriteListener configuration
1 2 3 4 5 6 7 |
@Bean public Step helloWorldStep(StepBuilderFactory stepBuilders) { return stepBuilders.get("myStep") .listener(new MyStepItemWriteListener())//add your Listener here .<Employee, String>chunk(10).reader(reader()) .processor(processor()).writer(writer()).build(); } |
Here is a simple implementation of MyStepItemWriteListener
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.List; import org.springframework.batch.core.ItemWriteListener; public class MyStepItemWriteListener implements ItemWriteListener<Number> { @Override public void beforeWrite(List<? extends Number> items) { System.out.println("Before write starts"); } @Override public void afterWrite(List<? extends Number> items) { System.out.println("After write ends"); } @Override public void onWriteError(Exception exception, List<? extends Number> items) { System.out.println("Exception on writting"); } } |
How to configure SkipListener
Here is the SkipListener configuration
1 2 3 4 5 6 7 |
@Bean public Step helloWorldStep(StepBuilderFactory stepBuilders) { return stepBuilders.get("myStep") .listener(new MyStepSkipListener())//add your Listener here .<Employee, String>chunk(10).reader(reader()) .processor(processor()).writer(writer()).build(); } |
Here is a simple implementation of MyStepSkipListener
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.springframework.batch.core.SkipListener; public class MyStepSkipListener implements SkipListener<String, Number> { @Override public void onSkipInRead(Throwable t) { System.out.println(" A failure on read"); } @Override public void onSkipInWrite(Number item, Throwable t) { System.out.println("This item failed on write "+item); } @Override public void onSkipInProcess(String item, Throwable t) { System.out.println("This item failed on processing "+item); } } |
DEMO
Finally, we implement a simple program that demonstrates two of the above listeners, MyJobListener
and MyStepListener
. Here we reuse source code from the post Spring Batch Hello World Example and put two listeners into HelloWorldJobConfig
class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.javabycode.batch; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.item.file.FlatFileItemReader; import org.springframework.batch.item.file.FlatFileItemWriter; import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; import org.springframework.batch.item.file.transform.PassThroughLineAggregator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import com.javabycode.listener.MyJobListener; import com.javabycode.listener.MyStepListener; import com.javabycode.model.Employee; @Configuration public class HelloWorldJobConfig { @Bean public Job helloWorlJob(JobBuilderFactory jobBuilders, StepBuilderFactory stepBuilders) { return jobBuilders.get("myJob") .listener(new MyJobListener())//add your Listener here .start(helloWorldStep(stepBuilders)).build(); } @Bean public Step helloWorldStep(StepBuilderFactory stepBuilders) { return stepBuilders.get("myStep") .listener(new MyStepListener())//add your Listener here .<Employee, String>chunk(10).reader(reader()) .processor(processor()).writer(writer()).build(); } @Bean public FlatFileItemReader<Employee> reader() { return new FlatFileItemReaderBuilder<Employee>() .name("employeeItemReader") .resource(new ClassPathResource("csv/employees.csv")) .delimited().names(new String[] {"firstName", "lastName","department"}) .targetType(Employee.class).build(); } @Bean public EmployeeItemProcessor processor() { return new EmployeeItemProcessor(); } @Bean public FlatFileItemWriter<String> writer() { return new FlatFileItemWriterBuilder<String>() .name("greetingItemWriter") .resource(new FileSystemResource( "target/output.txt")) .lineAggregator(new PassThroughLineAggregator<>()).build(); } } |
Running Application
To demo our Spring Batch Listeners Examples, we run the below command in the console.
1 |
mvn spring-boot:run |
Note: You must be in your project directory before run that command.
While running that command, you will see the output like below
That’s all about Spring Batch Listeners Examples. You may ben interested in the post Spring Batch Hello World Example.
If you find this post useful, don't hesitate to share it with your friends or other people for growing knowledge together and is an effort to contribute us.
References
Spring Boot Tutorial for Beginners
Spring Batch Frameworks