1. 基础部分
通过实例探讨”Hello World!”了解基本配置和实现
说明:
- 本实例使用的是spring-batch-2.2.7
- 本实例使用spring版本:3.2.9.RELEASE,包含:
- spring-core
- spring-beans
- spring-context
- spring-aop
- spring-tx
配置ItemReader、ItemProcessor和ItemWriter,完成”Hello World!”的输出
工程结构如下图:
+说明:
- JobLaunch.java:启动Bath
- writeTasklet.java:完成输出工作
- pom.xml:maven 配置
- applicationContext.xml:spring batch 基本配置
- batch_hello.xml:hello world step 任务配置
详细信息:
+pom.xml
<project xmlns=”http://maven.apache.org/POM/4.0.0” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd“>
<modelVersion>4.0.0</modelVersion>
<groupId>com.springbatch.hello</groupId>
<artifactId>SpringBatchHelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
</dependencies>
</project>
+JobLaunch.java
package org.june.springbatch.sample.helloworld;
/**
* Created by EX-XUJUNHONG010 on 2015-09-25.
*/
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JobLaunch {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("batch_hello.xml");
JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("helloWorldJob");
try {
/* 运行Job */
JobExecution result = launcher.run(job, new JobParameters());
/* 处理结束,控制台打印处理结果 */
System.out.println("result="+result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
+writeTasklet.java
package org.june.springbatch.sample.helloworld;
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;
/**
* Created by EX-XUJUNHONG010 on 2015-09-25.
*/
public class writeTasklet implements Tasklet {
/** Message */
private String message;
/**
* @param message
* the message to set
*/
public void setMessage(String message) {
this.message = message;
}
//@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
throws Exception {
System.out.println(message);
return RepeatStatus.FINISHED;
}
}
+applicationContext.xml
<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”
default-autowire=”byName”>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
</bean>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"></bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
</beans>
+batch_hello.xml
<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:batch=”http://www.springframework.org/schema/batch”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd“>
<import resource="applicationContext.xml"/>
<batch:job id="helloWorldJob">
<batch:step id="step_hello" next="step_world">
<batch:tasklet ref="hello" transaction-manager="transactionManager" />
</batch:step>
<batch:step id="step_world">
<batch:tasklet ref="world" transaction-manager="transactionManager" />
</batch:step>
</batch:job>
<bean id="hello" class="org.june.springbatch.sample.helloworld.writeTasklet">
<property name="message" value="Hello " />
</bean>
<bean id="world" class="org.june.springbatch.sample.helloworld.writeTasklet">
<property name="message" value=" World!" />
</bean>
</beans>
通过本例配置方式取得JobLauncher和Job对象,然后由JobLauncher的run方法启动job,参数JobParameters是标志job的一些参数,处理结束后,控制台输出处理结果