Wednesday, September 7, 2011

Getting Started With Spring batch 2.0

Hello world example using spring batch 2.0

Hello world example with spring batch 2.0 along with eclipse.
Follow below steps to start the hello world example with spring batch.

Step 1 : open eclipse - create new java project.
Step 2 : create new xml file under project (applicationContext.xml)
Step 3: create one more xml. under project location.(simpleJob.xml)
Step 4: create one java class which implements
org.springframework.batch.core.step.tasklet.Tasklet
Step 5: override the required methods in your respective class.
Step 6: we can run the job through
org.springframework.batch.core.launch.support.CommandLineJobRunner
This class will take two parameters (Job path and job parameters).
We need to pass those details.
Step 7: Here i am posting, how to run through eclipse.
Right click on the java class( Which we written)- Select RunConfigurations -
Select Main tab- Select Main class as
org.springframework.batch.core.launch.support.CommandLineJobRunner
Click on Arguments - Enter Jobpath and JobParameters under Program arguments
Eg: simpleJob.xml helloWorldJob
Click on the Run Button then hello world will display on the console.

Step 8: Required jar files
commons-logging-1.1.1.jar
javaee-api-5.0-1.jar
log4j-1.2.14.jar
spring-2.5.6.jar
spring-batch-core-2.1.8.RELEASE.jar
spring-batch-infrastructure-2.1.8.RELEASE.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-core-2.5.6.jar
spring-tx-2.5.6.jar
xstream-1.3.1.jar

In this way you can start the Spring batch.

Source Code of applicationContext.xml




 

 class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
   
  
 
    
  
  
Source Code of simpleJob.xml









Source Code of HelloWorld.java
package com.siva;
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 HelloWorld implements Tasklet {
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
throws Exception {
System.out.println("Hello World");
return RepeatStatus.FINISHED;
}
} 
Source Code of the log4j.properties

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c{1} %x - %m%n
log4j.logger.org.apache.commons=ERROR
log4j.logger.org.springframework=DEBUG
log4j.logger.org.springframework.security.ldap=DEBUG

AddToAny

Contact Form

Name

Email *

Message *