spring batch介绍(一)


        在现在社会的信息应用系统中,有很多数据需要在一定的时间窗口中进行加工。一个典型的用例应该是这样的:从一个系统导入数据,然后在另外一个系统中进行加工。比如说银行计算客户的存贷比,零售行业计算每天的营业额。这些都会用到批处理程序。不同于实时系统,批量处理程序不需要人为干预,处理的数据更多,对系统的效率、可靠性、健壮性要求更高。

        Spring Batch是一个旨在让企业级大量的数据批量处理变得更加得容易的Java开源框架。它能够更加可靠,更加高效的处理来自不同数据源(文件、数据库等)的大量数据。同时它是基于spring 以及pojo的,这对于程序员来说倍感亲切,不管是在学习的时候还是在应用它的时候。它基本上能够满足一般批处理系统的要求,让你开发批处理程序的时间更加简单,更加放心。

       下面以一个spring batch版本的hello world开始我们的spring batch之旅,这个任务只是简单的打印hello world。

  1. 首先你需要建一个maven的Java工程,并添加如下依赖。
        <dependency>
           <groupId>org.springframework.batch</groupId>
           <artifactId>spring-batch-core</artifactId>
           <version>3.0.1.RELEASE</version>
        </dependency>

                添加的依赖会给我们添加如下的jar文件,主要是spring batch的包,包括spring-batch-core, spring-batch-infrastructure以及spring的相关包。

libs

         2. 然后让我们写一个spring风格的类,并继承spring的tasklet类。tasklet就像它名字的意思一样,代表这一个小任务,可以把它看成是一个任务步骤。

package com.liang.batch;

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;
    }

    @Override
    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
			throws Exception {
        System.out.println(message);//打印注入的信息
        return RepeatStatus.FINISHED;//设置任务状态为完成
    }
}

       其中execute是我们需要实现的方法,会返回一个RepeatStatus的枚举值,这里我们打印一个消息,然后返回任务完成。

       3. 让我们在做一些工作,添加上面写的那个bean。在类路径下新建一个spring bean的配置文件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-4.0.xsd  
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/batch 
    http://www.springframework.org/schema/batch/spring-batch-3.0.xsd">

    <bean:bean id="hello" class="com.liang.batch.WriteTasklet">
        <bean:property name="message" value="Hello "></bean:property>
    </bean:bean>

    <bean:bean id="world" class="com.liang.batch.WriteTasklet">
        <bean:property name="message" value=" World!"></bean:property>
    </bean:bean>
    
    <!-- 
    <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:beans>
       注意下xmlns="http://www.springframework.org/schema/batch",说明在这个配置文件中,我们将batch作为我们的命名空间。所以我们在配置bean的时候需要在前面加入bean前缀。           

       4. 基础性的工作差不多完成,在加一些东西将这个任务运行起来。

           首先添加如下的配置文件batch-base.xml,并将我们上面的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-4.0.xsd  
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/batch 
    http://www.springframework.org/schema/batch/spring-batch-3.0.xsd">

    <bean:bean id="jobLaunch" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
         <bean:property name="jobRepository" ref="jobRepository"></bean:property>
    </bean:bean> 
    
    <bean:bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    </bean:bean>
    
    <bean:bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">
    </bean:bean>
    
    <bean:import resource="batch.xml"/>
</bean:beans>

      5.在batch.xml文件中,我们只是配置了hello, world的两个bean,问了让它们作为一个任务运行,需要在batch.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>
        在上面的配置中,我们配置了一个helloWorldJob, 并且有两个步骤。第一个步骤是打印hello ,strp_hello的下一个步骤是打印world。

      6. 我们在写一个main方法,将程序运行起来。

package com.liang.batch.JobLaunch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JobLaunch {
	public static void main(String[] args) {
        @SuppressWarnings("resource")
		ApplicationContext context = new ClassPathXmlApplicationContext(
                "batch-base.xml");
        //launcher是任务的启动者
        JobLauncher launcher = (JobLauncher) context.getBean("jobLaunch");
        //配置的hello world任务
        Job job = (Job) context.getBean("helloWorldJob");
        /* 运行Job */
        try {
        	//spring batch的任务都需要有一个参数,这里给它设置一个空参数。
        	JobParametersBuilder builder = new JobParametersBuilder();
        	JobParameters params = builder.toJobParameters();
            JobExecution result = launcher.run(job, params);
            /* 处理结束,控制台打印处理结果 */
            System.out.println(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

      下面是打印的日志,包括运行的任务,任务具体步骤,任务运行状态,参数等。

四月 15, 2015 9:47:21 下午 org.springframework.batch.core.launch.support.SimpleJobLauncher run
信息: Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters: [{}]
四月 15, 2015 9:47:21 下午 org.springframework.batch.core.job.SimpleStepHandler handleStep
信息: Executing step: [step_hello]
Hello
四月 15, 2015 9:47:21 下午 org.springframework.batch.core.job.SimpleStepHandler handleStep
信息: Executing step: [step_world]
 World!
四月 15, 2015 9:47:21 下午 org.springframework.batch.core.launch.support.SimpleJobLauncher run
信息: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
JobExecution: id=0, version=2, startTime=Wed Apr 15 21:47:21 CST 2015, endTime=Wed Apr 15 21:47:21 CST 2015, lastUpdated=Wed Apr 15 21:47:21 CST 2015, status=COMPLETED, exitStatus=exitCode=COMPLETED;exitDescription=, job=[JobInstance: id=0, version=0, Job=[helloWorldJob]], jobParameters=[{}]

      这里显示了任务运行的所有信息。好了,欢迎来到spring batch的世界。   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值