码迷,mamicode.com
首页 > 编程语言 > 详细

springbatch简介与helloworld

时间:2014-08-06 23:01:42      阅读:477      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   使用   os   io   

一、SpringBatch简介

Spring Batch是一个轻量级的批处理框架, 可以用于企业级海量数据处理, 它提供以下技术解决方案:

1. 定时批处理

2. 大规模并行处理

3. 企业消息驱动处理


二、SpringBatch结构

Spring Batch由应用层、核心层、基础架构层等组成:

1. 应用层: 包含所有的批处理作业,  使用spring框架管理程序员自定义的代码

2.核心层: 包含batch启动和控制所需要的核心类, 如: JobLauncher、Job、Setp等

3.基础架构层: 提供共通的读(ItemReader)、写(ItemWriter)和服务(RetryTemplate)

应用层和核心层简历在基础架构层之上, 下图展示了它们之间的关系:

bubuko.com,布布扣


三、SpringBatch流程

1. spring batch执行过程:

外部控制器调用JobLauncher启动一个Job, 每个batch都会包含一个Job, Job就像一个容器, 这个容器里装了若干个Setp, 

batch里面真正干活的就是这些Setp(ItemReader用来读取数据,ItemProcessor用来处理数据,ItemWriter用来写数据)

Job调用Step实现对数据的操作, Setp处理完成后, 将处理结果一步步返回给上一层。

JobRepository是上述处理提供的一种持久化机制, 它为JobLauncher、Job、Setp实例童工CRUD操作。

bubuko.com,布布扣

2. Step执行过程:
从DB或文件中取出数据的时候, read操作每次只读取一条记录, 然后将这条数据传递给processor处理, batch框架将重复做这两步操作,

直到读取记录的数量达到配置文件中"commin-interval"设定值得时候就会调用一个write操作, 然后再重复以上操作, 直到处理完所有的

数据。当这个Setp工作完成以后可以调到其他Setp或结束处理。

bubuko.com,布布扣

四、HelloWorld实例

本实例没有像前面讲的那样配置ItemReader、ItemProcessor、ItemWriter,而是直接在Setp中调用Tasklet

由Tasklet完成"Hello World!"的输出。

1. 工程结构图:

bubuko.com,布布扣


2. 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: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/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" />
	</bean>

	<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />

	<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
</beans>

applicationContext.xml主要用来配置一些spring信息, JobLaunch类用来启动Batch


3. springBatch.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: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/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.1.xsd">

	<!-- 装载spring核心配置文件 -->
	<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="com.zdp.springbatch.WriteTasklet">
		<bean:property name="message" value="Hello "></bean:property>
	</bean:bean>

	<bean:bean id="world" class="com.zdp.springbatch.WriteTasklet">
		<bean:property name="message" value=" World!"></bean:property>
	</bean:bean>
</bean:beans>

springBatch.xml配置了一个ID为helloWorldJob的Job,这个Job有两个Setp:setp_hello和setp_world,

前者负责输出“Hello ”, 后者负责输出“World!”,当第一个Setp完成之后执行第二个Setp。


4. WriteTasklet:

public class WriteTasklet implements Tasklet {

    private String message;

    /**
     * @param message
     * the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext)throws Exception {
        System.out.println(message);
        return RepeatStatus.FINISHED;
    }

}
WriteTasklet中定义了一个message属性,通过springBatch.xml的hello和world bean为其注入值,execute方法由Tasklet接口继承而来,

是Tasklet实现业务逻辑的地方,此实例只是简单的输出message信息后直接返回。


5. JobLaunch

/**
 * Test client
 */
public class JobLaunch {

	public static void main(String[] args) {
		try {
			ApplicationContext context = new ClassPathXmlApplicationContext("springBatch.xml");

			JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
			Job job = (Job) context.getBean("helloWorldJob"); 

			// JobLauncher可以用来启动Job
			JobExecution result = jobLauncher.run(job, new JobParameters());
			
			// 处理结束,控制台打印处理结果 
			System.out.println(result.toString());
		} catch (Exception e) {
			throw new RuntimeException("error happens...", e);
		}
	}
}
通过spring配置取得JobLauncher和Job对象,然后由JobLauncher的run方法启动Job,JobParameters是标志Job的一些参数,

处理结束后控制台输出处理结果。
转自:http://www.cnblogs.com/gulvzhe

springbatch简介与helloworld,布布扣,bubuko.com

springbatch简介与helloworld

标签:style   blog   http   color   java   使用   os   io   

原文地址:http://blog.csdn.net/zdp072/article/details/38406721

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!