Saturday, 19 January 2013

Spring Batch Chunk Oriented Processing

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 ItemProcessor{

 public Staff process(Staff staff) throws Exception {
  // TODO Auto-generated method stub
  System.out.println(staff);
  return staff;
 }
 

}
and finaly running the application
 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




Friday, 18 January 2013

Spring Batch Tutorials

Spring Batch

Spring batch is framework for batch jobs based on spring framework. Spring batch don't have scheduler by default . But its possible to integrate with any scheduler framework or OS based scheduler eg crone tab or windows scheduler. If need more details about spring please refer http://static.springsource.org/spring-batch/ For this tutorials i am using using maven to build the project.
Following are the dependecies.  

4.0.0

  edu.ilovejava
  edu.ilovejava
  0.0.1-SNAPSHOT
  jar

  edu.ilovejava
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      3.8.1
      test
    
    
    org.springframework.batch
    spring-batch-core
    2.1.9.RELEASE

  

Next Hello Step1 program .

We are doing two ways to do hello Step1 program . First with using implements Tasklet and second approach without implement any interface from batch. I prefer second approach. Following are the code for HelloStep1.java
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

public class HelloStep1  implements Tasklet {

 public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
   throws Exception {
  System.out.println("Hello step 1 finished");
  return RepeatStatus.FINISHED;
 }

}
Next is Heart of the Spring Batch
Creation of Apllication Context
Following are the Application Context (context.xml)  xml code.




 

  

  

 



 

 

  

 



 
 
 

 
  
  
 

 
  
  
   
    
     
    
    
     
    
   
  
  
 
The Main class is org.springframework.batch.core.job.SimpleJob all tasklet you declared in the step property it will run sequentially. I declared two bean called hello1 and hello2.
It will run both class when i start batch job.

Next Step Running Job

following class using for running job with parameter as applicaton context file name and job id org.springframework.batch.core.launch.support.CommandLineJobRunner
Its possible to run the above job using maven too .
 mvn exec:java -Dexec.mainClass=org.springframework.batch.core.launch.support.CommandLineJobRunner -Dexec.args="context.xml footballJob"
Next Helloworld program without implementing Taklet
Following are the hello world program

public class HelloWorld {

 public void hello()
 {
  System.out.println("Hellow World Testing");
 }
 

}
The following are spring application context code to run the job. Its quite easy to configure and run . Its able to configure which method need to invoke when batch job called. As per the example it will run hello method from HelloWorld Class.

 

  

   

    

     

     

    

   

  

 
And finally running the job using maven.
 mvn exec:java -Dexec.mainClass=org.springframework.batch.core.launch.support.CommandLineJobRunner -Dexec.args="context.xml helloWorld"
Source code available in Google Code. http://code.google.com/p/spring-batch-tutorials/downloads/list

Download GreenMangoQuery java swing based client for MongoDB > Download Green MangoQuery

Download MongoDB Java Swing based client