Skipping records during record reading, processing or writing

While reading a record from a file, processing already read record, or writing a record to a file, exceptions happens. Sometimes the exceptions are not so serious and we can skip it. We can configure the Spring Batch to skip the current record if an exception happens. As shown below XML code 1 <batch:job id=”importEmployees”>…… Continue reading Skipping records during record reading, processing or writing

JsonLineMapper Example

In this post under Spring Batch, I will explain the purpose of JsonLineMapper with an example. JsonLineMapper is used with reader bean for reading file containing JSON input as shown below JsonFileInput.txt {“id”:”id0″,”name”:”name0″,”status”:”status0″,”salary”:0}{“id”:”id1″,”name”:”name1″,”status”:”status1″,”salary”:1}{“id”:”id2″,”name”:”name2″,”status”:”status2″,”salary”:2}{“id”:”id3″,”name”:”name3″,”status”:”status3″,”salary”:3}{“id”:”id4″,”name”:”name4″,”status”:”status4″,”salary”:4}{“id”:”id5″,”name”:”name5″,”status”:”status5″,”salary”:5}{“id”:”id6″,”name”:”name6″,”status”:”status6″,”salary”:6}{“id”:”id7″,”name”:”name7″,”status”:”status7″,”salary”:7}{“id”:”id8″,”name”:”name8″,”status”:”status8″,”salary”:8}{“id”:”id9″,”name”:”name9″,”status”:”status9″,”salary”:9} Each line in the file is interpreted as a JSON object by JsonLineMapper. So each line must start with “{” and end…… Continue reading JsonLineMapper Example

RecordSeparatorPolicy interface Example

In this post of Spring Batch, I will explain with an example, the purpose of RecordSeparatorPolicy interface and how to use it. In all my previous posts under Spring Batch, the examples (i.e., input file to read from) that I used, had a collection of records, where each record were delimited by new line as…… Continue reading RecordSeparatorPolicy interface Example

Transforming items using ItemProcessor

In this post under Spring Batch, I will explain how to achieve Item Transformation using ItemProcessor interface. Item Transformation process transforms the items read by reader before sending them to the writer. The transformation involves changing the state of the read itemor creating a completely new object. In the second case, the written item type…… Continue reading Transforming items using ItemProcessor

Using custom delimiter while reading and writing batch file

In all previous Spring Batch posts, the DelimitedLineAggregator(which creates String format of java objects) and DelimitedLineTokenizer (which extracts fields from each record) assume ‘,’ as delimiter as shown below EMP_ID0,EMP_NAME0,EMP_STATUS0,0EMP_ID1,EMP_NAME1,EMP_STATUS1,1EMP_ID2,EMP_NAME2,EMP_STATUS2,2EMP_ID3,EMP_NAME3,EMP_STATUS3,3 We can use other symbols as delimiter and inform DelimitedLineAggregator and DelimitedLineTokenizer about this. At the time of reading and writing, the default delimiter is…… Continue reading Using custom delimiter while reading and writing batch file

Validating JobParameters using JobParametersValidator interface

In this post under Spring Batch, I will explain how to validate the parameters passed to the job instance. The interface we will be using for validating the Job parameters is org.springframework.batch.core.JobParametersValidator which has only one method as shown below public interface JobParametersValidator { void validate(JobParameters parameters) throws JobParametersInvalidException; } Based on the signature of…… Continue reading Validating JobParameters using JobParametersValidator interface

Passing Job Parameters using JobParametersBuilder

In this post under Spring Batch I will explain how to pass parameters to a job. We will take the help of org.springframework.batch.core.JobParametersBuilder class which follows Builder design pattern. We will use JobParametersBuilder class to build the job parameters. Then we pass the job parameters along with the job (to be executed) as parameters to…… Continue reading Passing Job Parameters using JobParametersBuilder

Chaining multiple ItemProcessor

In this post of Spring Batch, I will explain with an example how to chain multiple ItemProcessors. For my example, I will create an ItemProcessor which will go through employee records one item at a time and filter out odd records. Below is the code of the ItemProcessor package xml.package11; import org.springframework.batch.item.ItemProcessor; public class EmployeeFilterItemProcessor…… Continue reading Chaining multiple ItemProcessor

ItemReadListener, ItemWriteListener, and ItemProcessListener Example

In this post under Spring Batch, I will explain how to create listeners during 1) reading of an item2) writing of an items3) processing of an item with an example. We create listeners by implementing the three interfaces ItemReadListener, ItemWriteListener, and ItemProcessListener. The class that implements ItemReadListener<T> interface must provide implementation for the below three…… Continue reading ItemReadListener, ItemWriteListener, and ItemProcessListener Example