基于xml方式配置spring batch

从平面文件(Flat File)中批量读取数据 章节用的是编码方式配置spring batch的job任务的,本节就是把上篇博客中job任务改成基于xml配置的方式配置job,因为公司中现在很大一部分还是用的xml方式配置job。

案例:还是读取User.txt文件中数据,并把每条数据中满足年纪是偶数的打印出来。

1、创建spring boot工程,并在pom.xml文件中引入所需要的jar包

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> 
            <scope>runtime</scope> </dependency> -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

2、读文件
spring batch提供了FlatFileItemReader,用来读取文件,把每条数据映射到User对象中,通过下面方式映射

package com.lzj.batch;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

public class UserFieldSetMapper implements FieldSetMapper<User> {

    @Override
    public User mapFieldSet(FieldSet fieldSet) throws BindException {
        // TODO Auto-generated method stub
        return new User(fieldSet.readString("ID"),
                fieldSet.readString("NAME"), 
                fieldSet.readString("AGE"));
    }

}

3、处理
对每条数据进行处理,实现ItemProcessor即可

package com.lzj.batch;
import org.springframework.batch.item.ItemProcessor;
public class UserItemProcessor implements ItemProcessor<User, User> {

    @Override
    public User process(User item) throws Exception {
        if (Integer.parseInt(item.getAge()) % 2 == 0) {
            return item;
        }
        return null;
    }

}

4、写数据
实现ItemWriter即可

package com.lzj.batch;
import java.util.List;
import org.springframework.batch.item.ItemWriter;
public class UserWriter implements ItemWriter<User> {

    @Override
    public void write(List<? extends User> items) throws Exception {
        for(User user : items){
            System.out.println(user);
        }
    }

}

5、配置Job作业

<?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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:task="http://www.springframework.org/schema/task"
    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
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd">

    <bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="resource" value="data/User.txt"></property>
        <property name="lineMapper" ref="lineMapper"></property>
        <property name="linesToSkip" value="1"></property>
    </bean>

    <bean id="lineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
        <property name="lineTokenizer" ref="lineTokenizer"></property>
        <property name="fieldSetMapper" ref="fieldSetMapper"></property>
    </bean>

    <bean id="lineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
        <property name="names" value="ID,NAME,AGE"></property>
    </bean>
    <bean id="fieldSetMapper" class="com.lzj.batch.UserFieldSetMapper"></bean>

    <bean id="process" class="com.lzj.batch.UserItemProcessor"></bean>
    <bean id="write" class="com.lzj.batch.UserWriter"></bean>

    <!-- 选用内存级别的数据库,不在需要用oracle、mysql等数据库,采用h2数据库,不需要手动配置,只需要的pom.xml文件中引入h2即可 -->
    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
        <property name="transactionManager" ref="transactionManager"></property>
    </bean>
    <bean id="executor" class="org.springframework.core.task.SimpleAsyncTaskExecutor"></bean>
    <bean id="laucher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository"></property>
        <property name="taskExecutor" ref="executor"></property>
    </bean>
    <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/> 

    <batch:job id="jobExample">
        <batch:step id="stepExample">
            <batch:tasklet>
                <batch:chunk reader="reader" processor="process" writer="write"
                    commit-interval="3">
                </batch:chunk>
            </batch:tasklet>
        </batch:step>
    </batch:job>

</beans>

6、run,启动job

public class DemoApplication {

    public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("job.xml");

        SimpleJobLauncher launcher = (SimpleJobLauncher) ctx.getBean("laucher");
        Job job = (Job) ctx.getBean("jobExample");
        System.out.println(launcher);
        System.out.println(job);
        launcher.run(job, new JobParameters());
        ctx.close();

    }
}

工程目录
这里写图片描述

源码下载位置:https://github.com/shuniversity/spring-batch-demo

猜你喜欢

转载自blog.csdn.net/u010502101/article/details/79944314