springbatch学习笔记1

1.什么是spring-batch?
spring-batch是一个轻量级的批处理框架。旨在开发对企业系统的正常运行至关重要的批处理应用程序。
spring-batch提供了可重用的功能,这些功能对于处理大量记录至关重要,包括日志记录/跟踪,事务管理,
作业处理统计信息,作业重新启动,跳过和资源管理。
无论是简单还是复杂,大批量处理作业都可以以高扩展的方式来利用框架处理大量信息

2.spring-batch的重点概念!!!
使用spring-batch必须要理解的几个概念:
1.作业(Job):Job是封装整个批处理过程的实体,一个job可以包含多个步骤
2.步骤(Step):Step就是作业执行的步骤流程,步骤之间可以并行
3.作业调度器(JobLauncher):通过框架执行作业
4.作业仓库(JobRepository):用来存储Job的元数据(支持内存,DB两种模式)
一个典型的Job包含:作业读(Reader),作业处理(Process),作业写(Writer)三步式架构。整个批处理框架也基本围绕Reader,Process,Writer来处理。
除此之外,框架还提供了作业调度器(JobLauncher),作业仓库(JobRepository)

2.1:Job
Job是封装整个批处理过程的实体,job要与xml配置或者java配置连接在一起,
JobInstance: 逻辑作业运行的概念
JobParameters: 用来区分用一个Job的不同的JobInstance,
JobInstance = Job + JobParameters
假设现在有一个作业 Job 为打印每天的数据信息,那么1月1日是一个实例,1月2日也是一个实例,这些实例都属于 Job ,但是通过时间日期来区分,因此
日期就是 JobParameters
JobExecution: Job的一次尝试运行,如果运行不成功,再次尝试运行时,就会创建一个新的JobExecution,但是JobInstance只有一个
2.2: Step
Step是Job中的步骤,作业执行的流程,每一个step都应包含reader,process,writer三个步骤
2.3:Tasklet: 任务单元
2.4:Chunk: 表示在提交事务之前,需要处理的项目数
写一个springbatch的简单应用:控制台打印 hello world
构建项目引入jar包:
           <dependency>
                <groupId>org.springframework.batch</groupId>
                <artifactId>spring-batch-core</artifactId>
                <version>LATEST</version>
            </dependency>    

编辑代码:写数据的任务单元

package com.zs.springbatch.config;

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;

/**
 * @Company lhfinance.com
 * @Author ZhaoShuai
 * @Date Create in 2019/12/5
 **/
public class WriteTasklet implements Tasklet {

    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

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

编辑spring配置文件:application.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:batch="http://www.springframework.org/schema/batch"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">

    <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"></bean>
   
    <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="com.zs.springbatch.config.WriteTasklet">
        <property name="message" value="hello"/>
    </bean>
    <bean id="world" class="com.zs.springbatch.config.WriteTasklet">
        <property name="message" value="world"/>
    </bean>
</beans>

4.编辑测试类,运行测试:

public class TestDemo {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        JobLauncher bean = context.getBean(JobLauncher.class);
        Job job = (Job) context.getBean("helloWorldJob");
        try {
            JobExecution result = bean.run(job, new JobParameters());
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行结果:

十二月 13, 2019 9:19:09 上午 org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet
信息: No TaskExecutor has been set, defaulting to synchronous executor.
十二月 13, 2019 9:19:09 上午 org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
信息: Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters: [{}]
十二月 13, 2019 9:19:09 上午 org.springframework.batch.core.job.SimpleStepHandler handleStep
信息: Executing step: [step_hello]
hello
十二月 13, 2019 9:19:09 上午 org.springframework.batch.core.step.AbstractStep execute
信息: Step: [step_hello] executed in 49ms
十二月 13, 2019 9:19:09 上午 org.springframework.batch.core.job.SimpleStepHandler handleStep
信息: Executing step: [step_world]
world
十二月 13, 2019 9:19:09 上午 org.springframework.batch.core.step.AbstractStep execute
信息: Step: [step_world] executed in 10ms
十二月 13, 2019 9:19:09 上午 org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
信息: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 140ms

从上面可以看出,在配置中,hello是一个任务单元,world是一个任务单元,因此先执行了hello,后执行了world,控制台打印信息可以看出运行的步骤等

原文地址:https://www.cnblogs.com/Zs-book1/p/12033286.html