【Spring Batch学习笔记】1:基本使用流程例子

输出HelloWorld的例子,参考1,2

程序结构

这里写图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.lzh</groupId>
    <artifactId>testSpringBatch</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.batch/spring-batch-core -->
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>3.0.7.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--jobLauncher负责batch的启动工作-->
    <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <!--注入jobRepository-->
        <property name="jobRepository" ref="jobRepository"/>
    </bean>
    <!--jobRepository负责job的整个运行过程中的CRUD操作-->
    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/>
    <!--transactionManager负责事务的管理操作-->
    <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
</beans>

batch.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">
    <!--导入前面的Spring配置文件-->
    <import resource="applicationContext.xml"/>

    <!--这里要用[batch:]作为前缀是因为在前面命名空间(xmlns)中使用了[xmlns:batch]为其指定了命名空间-->
    <batch:job id="helloWorldJob">
        <!--一个job中有若干step作为这个作业的作业步-->
        <!--step的[id属性]标识这个step,[next属]性标识其下一个step的id-->
        <batch:step id="step_hello" next="step_word">
            <!--tasklet和chunk是用来指定处理过程的-->
            <!--一个tasklet对应于一个事务性的、潜在可重复的过程中发生的一个步骤-->
            <!--可以自己实现Tasklet接口,定义自己的tasklet。如:用于解压文件,执行系统命令,执行存储过程等-->
            <!--[ref属性]指明这个tasklet的实现bean,[transaction-manager属性]指明其事务管理器,这里使用了前面定义的Spring配置文件中的事务管理器-->
            <batch:tasklet ref="hello" transaction-manager="transactionManager"/>
        </batch:step>
        <!--另一个作业步,已经是最后一个了,所以不用[next属性]-->
        <batch:step id="step_word">
            <batch:tasklet ref="world" transaction-manager="transactionManager"/>
        </batch:step>
    </batch:job>

    <!--以下两个是前面的作业步的tasklet的实现bean-->
    <bean id="hello" class="org.lzh.PrintTasklet">
        <!--setter方式注入属性-->
        <property name="msg" value="你好"/>
    </bean>

    <bean id="world" class="org.lzh.PrintTasklet">
        <property name="msg" value="LZH"/>
    </bean>

</beans>

PrintTasklet类

package org.lzh;

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;

//自定义的tasklet需要是实现Tasklet接口及其execute方法
public class PrintTasklet implements Tasklet {
    //自定义的一个属性,用来记录传进来的信息
    private String msg;

    //用setter方式传入信息(从batch.xml中给bean设置property子标签)
    public void setMsg(String msg) {
        this.msg = msg;
    }

    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
        //这个tasklet做的唯一事情就是将保存的信息标准输出
        System.out.println(this.msg);
        //RepeatStatus.FINISHED表示该step执行完毕
        return RepeatStatus.FINISHED;
    }
}

Main类

package org.lzh;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        //batch.xml导入了applicationConext.xml,所以只写batch.xml就可以一并加入上下文
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("batch.xml");
        //Spring Batch的作业启动器,在applicationContext.xml中配置为了bean
        JobLauncher jobLauncher = (JobLauncher) applicationContext.getBean("jobLauncher");
        //在batch.xml中配置的一个作业
        Job job = (Job) applicationContext.getBean("helloWorldJob");


        try {
            //开始执行这个作业,获得处理结果(要运行的job,job参数对象)
            JobExecution result = jobLauncher.run(job, new JobParameters());
            //输出处理结果看一下
            System.out.println("处理结果:" + result.toString());
        } catch (JobExecutionAlreadyRunningException e) {
            e.printStackTrace();
        } catch (JobRestartException e) {
            e.printStackTrace();
        } catch (JobInstanceAlreadyCompleteException e) {
            e.printStackTrace();
        } catch (JobParametersInvalidException e) {
            e.printStackTrace();
        }

    }
}

运行结果

这里写图片描述

猜你喜欢

转载自blog.csdn.net/SHU15121856/article/details/81325855