Spring Batch Example – Hello World Project
Create a Mavenproject by Eclipse
New Project- Maven– QuickStart – GroupID(SpringBatchHelloWorld),articficateID(com.ermdashboard.SpringBatchHelloWorld)
Src/main/java/com.ermdashboard.SpringBatchHelloWorld,create Java Code:
packagecom.ermdashboard.SpringBatchHelloWorld;
import org.springframework.batch.core.Job;
importorg.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
importorg.springframework.batch.core.launch.JobLauncher;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
public class JobLaunch {
/**
* @param args
*/
public static void main(String[] args) {
String[]springConfig =
{ "application.xml",
"batch.xml"
};
ApplicationContext context = newClassPathXmlApplicationContext(springConfig);
JobLauncher launcher = (JobLauncher)context.getBean("jobLauncher");
Job job = (Job) context.getBean("helloWorldJob");
try {
/* 运行Job */
JobExecution result =launcher.run(job, newJobParameters());
/* 处理结束,控制台打印处理结果 */
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
packagecom.ermdashboard.SpringBatchHelloWorld;
importorg.springframework.batch.core.StepContribution;
importorg.springframework.batch.core.scope.context.ChunkContext;
importorg.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
public class writeTasklet implements Tasklet {
/** Message*/
private String message;
/**
* @param message
* the message to set
*/
public void setMessage(String message) {
this.message =message;
}
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
throws Exception {
System.out.println(message);
return RepeatStatus.FINISHED;
}
}
Pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">