在看本篇博客之前,希望您能先到http://xuanmeiku.taobao.com去转转,里面全是真皮炫美酷时尚女鞋,价格实惠!如果你看中了哪一款,可以加我qq1074992674,或者直接通过旺旺联系我!欢迎大家的骚扰!本人诚信经营,绝不做欺骗他人的事情!
通过前面两篇关于Spring Batch文章的介绍,我们已经对Spring Batch有个初步的概念。本篇文章我们将通过一个“Hello World!”实例,从开发的角度对Spring Batch有一个真切的体会。
说明:
1、本实例使用的是spring-batch 2.2.2;
2、本实例没有像前面讲的那样配置ItemReader、ItemProcessor和ItemWriter,而是之间在Step中调用了Tasklet,由Tasklet完成“Hello World!”的输出;
3、本实例是采用Maven构建的;
工程结构如下图:
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>springbatch</groupId>
<artifactId>springbatch-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springbatch-helloworld</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
</dependencies>
</project>
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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"></property>
</bean>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"></bean>
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"></bean>
</beans>
batch.xml的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:bean="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">
<bean:import resource="applicationContext.xml"/>
<job id="helloWorldJob">
<step id="step_hello" next="step_world">
<tasklet ref="hello" transaction-manager="transactionManager"></tasklet>
</step>
<step id="step_world">
<tasklet ref="world" transaction-manager="transactionManager"></tasklet>
</step>
</job>
<bean:bean id="hello" class="cn.lichunan.springbatch.writer.WriteTasklet">
<bean:property name="message" value="Hello "></bean:property>
</bean:bean>
<bean:bean id="world" class="cn.lichunan.springbatch.writer.WriteTasklet">
<bean:property name="message" value=" World!"></bean:property>
</bean:bean>
</bean:beans>
配置了一个ID为helloWorldJob的job,此job有两个Step:step_hello和step_world,前者负责输出“Hello”,后者负责输出“World!”,当第一个Step完成以后,执行第二个Step。
WriteTasklet类的代码如下:
package cn.lichunan.springbatch.writer;
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 WriteTasklet implements Tasklet
{
private String message;
public void setMessage(String message) {
this.message = message;
}
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
throws Exception {
System.out.println("->" + message);
return RepeatStatus.FINISHED;
}
}
此类中定义了一个message属性,通过batch.xml的“hello”和"world"Bean为其注入值。execute方法,是由于Tasklet接口继承而来的,是Tasklet实现业务逻辑的地方,此实例只是简单的输出Message信息后,直接返回。
测试类的代码如下:
package cn.lichunan.springbatch.writer;
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;
import junit.framework.TestCase;
public class HelloWorldTest
extends TestCase
{
public void testPrintHelloWorld(){
ApplicationContext context = new ClassPathXmlApplicationContext("batch.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.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
本例通过Spring配置的方式取得JobLauncher和Job对象,然后由JobLauncher的run方法启动job,参数JobParameters是标志job的一些参数,处理结束后,控制台输出处理结果。
上面是通过SpringBatch运行一个“Hello World!”程序所需要的基本配置。由于其优势是处理大批量的数据,所以仅仅为了输出“Hello World!”而编写这么多代码和配置文件,确实显得有些笨拙,也体现不出其优越性。