Spring Batch Example ? Hello World Project (一)

2014-11-24 09:49:26 · 作者: · 浏览: 3

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">

4.0.0

com.ermdashboard

SpringBatchHelloWorld

0.0.1-SNAPSHOT

jar

SpringBatchHelloWorld

http://maven.apache.org

UTF-8

UTF-8

1.6

3.2.2.RELEASE

2.2.0.RELEASE

org.springframework

spring-core

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework.batch

spring-batch-core

${spring.batch.version}

org.springframework.batch

spring-batch-infrastructure

${spring.batch.version}

com.oracle

ojdbc14

10.2.0.3.0

junit

junit

3.8.1

test