Spring Batch provides a facility to add listeners at job level. The listeners are executed before the job is started and after the job is finished. This post explains how to create listeners and integrate them with the job. We can add our custom processing logic in these listeners and expect them to be called…… Continue reading Adding job level listeners using JobExecutionListener
Creating a temporary file and directory
The post shows you how to create a temporary file and directory in java with an example. Main Code 1 package nio; 2 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Path; 6 import java.nio.file.Paths; 7 8 public class Example1 { 9 public static void main(String[] args) throws IOException { 10 Path path = Files.createTempFile(“prefix”,…… Continue reading Creating a temporary file and directory
Partitioning a list
Apache Common Collections framework provide a facility to divide a given list into multiple sub list based on a given size. The final list may be smaller than the give size. The below code will show how to do it. 1 import java.util.ArrayList; 2 import java.util.List; 3 4 import org.apache.commons.collections4.ListUtils; 5 6 public class Example5…… Continue reading Partitioning a list
Merging two sorted collections
Apache Common Collections API provide a facility to merge two sorted collections. This post will show how to use it with an example Main Code 1 import java.util.ArrayList; 2 import java.util.List; 3 4 import org.apache.commons.collections4.CollectionUtils; 5 6 public class Example3 { 7 public static void main(String[] args) { 8 List list1 = new ArrayList(); 9…… Continue reading Merging two sorted collections
BidiMap Example
Apache Common Collections framework provide a new map collection called “BidiMap”. This collection is similar to map and at the same time it is different. Similarities 1) Both will contain list of entries each of key value pair 2) By using key we can retrieve the corresponding value Differences 1) In BidiMap we can invert…… Continue reading BidiMap Example
Bag Collection
A bag or multiset is a collection that allow duplicate items. It also keeps count of how many duplicate items are present. Apache Collections framework provide an implementation of Bag collection. Below is an example of it. Bag example 1 import org.apache.commons.collections4.Bag; 2 import org.apache.commons.collections4.bag.HashBag; 3 4 public class Example1 { 5 public static void…… Continue reading Bag Collection
FlatFileHeaderCallback and FlatFileFooterCallback example
In this post I will explain how to add header and footer to the destination file created by Spring batch. When Spring batch transfer records from source to destination file in batch, it provides facility through which the programmer can provide header and footer information thathas to added in the destination file. To take advantage…… Continue reading FlatFileHeaderCallback and FlatFileFooterCallback example
Custom FieldSetMapper example
In the previous post “Spring Batch Simple Example” we used “BeanWrapperFieldSetMapper” while reading the employee records from the file. The BeanWrapperFieldSetMapper created a newinstance of Employee class for every employee record in the file and set the instance fields. We can have more control on the task performed by BeanWrapperFieldSetMapper by providing a custom implementation…… Continue reading Custom FieldSetMapper example
Spring Batch Simple Example Part 3
In this post I will explain how to combine the reader and writer in a job and how to run the job. We define a job which will transfer the employee records from one file to another file in batches. <batch:job id=”importEmployees”> <batch:step id=”readWriteEmployees”> <batch:tasklet> <batch:chunk reader=”reader” writer=”writer” commit-interval=”50″/> </batch:tasklet> </batch:step> </batch:job> <bean id=”transactionManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>…… Continue reading Spring Batch Simple Example Part 3
Spring Batch Simple Example Part 2
In this post, I will explain how to write the employee objects created in the reading process to a file. To write the list of employee records in batches, we need to create an instance of org.springframework.batch.item.file.FlatFileItemWriter. It takes two information1) the resource file where the information has to be written2) An instance of LineAggregator…… Continue reading Spring Batch Simple Example Part 2