Chunk Oriented Processing .
Please refer my previous tutorials if you are new to Spring Batch.http://greenspringjava.blogspot.sg/2013/01/blog-post.html
Chunk oriented processing have three components , ItemReader , ItemWriter and ItemProcessor.
Following are the use case for chunk oriented processing.
Read test.csv file and convert into object and writing into another file output.csv with tabular format.
ItemReader
Read test.csv file based and convert into Object name called Staff. For reading file, its use FlatFileItemReader from spring batch.
Following are the code in context.xml to convert flat file into Staff Object.
Following are the code fieldSetMapper used for converting each line of csv file in to Staff object.
package edu.ilovejava.batch.chunk;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
public class FieldMapper implements FieldSetMapper{
public Object mapFieldSet(FieldSet field) throws BindException {
Staff staff = new Staff();
staff.setName(field.readString(0));
staff.setCountry(field.readString(1));
staff.setSalary(field.readDouble(2));
staff.setGender(field.readString(3));
staff.setPosition(field.readString(4));
// TODO Auto-generated method stub
return staff;
}
}
Code in context.xml file for writing the file in tabular format.
bean id="writer" class="org.springframework.batch.item.file.FlatFileItemWriter">In this example not using any conversion in processor, just print Object .
package edu.ilovejava.batch.chunk; import org.springframework.batch.item.ItemProcessor; public class ChunkProcessor implements ItemProcessorand finaly running the application{ public Staff process(Staff staff) throws Exception { // TODO Auto-generated method stub System.out.println(staff); return staff; } }
mvn exec:java -Dexec.mainClass=org.springframework.batch.core.launch.support.CommandLineJobRunner -Dexec.args="context.xml chunk"The above example able to download from google code. http://code.google.com/p/spring-batch-tutorials/downloads/list http://code.google.com/p/spring-batch-tutorials/downloads/detail?name=edu.ilovejava_20_01_13.zip&can=2&q=#makechanges
No comments:
Post a Comment