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

No comments:

Post a Comment

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

Download MongoDB Java Swing based client