Spring Batch_官網DEMO實現

jopen 10年前發布 | 80K 次閱讀 Spring JEE框架 Spring Batch

Spring Batch_官網DEMO實現

http://spring.io/guides/gs/batch-processing/

使用spring xml方式實現了spring batch官網的demo,現在把具體的代碼貼出來,具體的細節配置還要參考官網的說明。

首先建立maven項目,pom文件如下:

<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>lyx</groupId>
    <artifactId>SpringBatch2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!--spring context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>

        <!--spring core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>

        <!--spring bean -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>

        <!--spring aop -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>

        <!--spring jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>

        <!-- spring tx -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>3.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
            <version>8.0.14</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

    </dependencies>
</project>

 

最主要的就是配置下面的這個spring-batch2.xml文件,也就是配置reader,writer,processor,以及各種依賴類。該spring-batch2.xml文件時依據BatchConfiguration.java配置的,具體配置如下:

<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-4.0.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">

    <bean id="lineTokenizer"
        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
        <property name="delimiter" value="," />
        <property name="names">
            <list>
                <value>firstName</value>
                <value>lastName</value>
            </list>
        </property>
    </bean>
    <bean id="fieldSetMapper"
        class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
        <property name="prototypeBeanName" value="person" />
    </bean>
    <bean id="person" class="com.lyx.batch.Person" scope="prototype" />
    <bean id="lineMapper"
        class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
        <property name="lineTokenizer" ref="lineTokenizer" />
        <property name="fieldSetMapper" ref="fieldSetMapper" />
    </bean>
    <bean id="resource" class="org.springframework.core.io.ClassPathResource">
        <constructor-arg index="0" type="java.lang.String"
            value="sample-data.csv" />
    </bean>
    <bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="resource" ref="resource" />
        <property name="encoding" value="utf-8" />
        <property name="lineMapper" ref="lineMapper" />
    </bean>

    <bean id="itemProcessor" class="com.lyx.batch.PersonItemProcessor" />
    <bean id="jdbcBatchItemWriter"
        class="org.springframework.batch.item.database.JdbcBatchItemWriter">
        <property name="itemSqlParameterSourceProvider" ref="itemSqlParameterSourceProvider" />
        <property name="sql"
            value="INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="itemSqlParameterSourceProvider"
        class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
    <!--tomcat jdbc pool數據源配置 -->
    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
        destroy-method="close">
        <property name="poolProperties">
            <bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
                <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/test" />
                <property name="username" value="root" />
                <property name="password" value="034039" />
                <property name="maxActive" value="100" />
                <property name="initialSize" value="10" />
                <property name="maxWait" value="10000" />
                <property name="minIdle" value="10" />
            </bean>
        </property>
    </bean>

    <batch:job id="sampleJob" job-repository="jobRepository">
        <batch:step id="step">
            <tasklet transaction-manager="transactionManager">
                <chunk reader="flatFileItemReader" processor="itemProcessor"
                    writer="jdbcBatchItemWriter" commit-interval="1" />
            </tasklet>
        </batch:step>
    </batch:job>

    <!-- spring batch 配置jobRepository -->
    <batch:job-repository id="jobRepository"
        data-source="dataSource" transaction-manager="transactionManager"
        isolation-level-for-create="REPEATABLE_READ" table-prefix="BATCH_"
        max-varchar-length="1000" />
    <!-- spring的事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- batch luncher -->
    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>
</beans>

主要的類有以下兩個類:

Person.java

package com.lyx.batch;

public class Person {
    private String lastName;
    private String firstName;

    public Person() {
    }

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "firstName: " + firstName + ", lastName: " + lastName;
    }

}

PersonItemProcessor.java

package com.lyx.batch;

import org.springframework.batch.item.ItemProcessor;

public class PersonItemProcessor implements ItemProcessor<Person, Person> {

    public Person process(final Person person) throws Exception {
        final String firstName = person.getFirstName().toUpperCase();
        final String lastName = person.getLastName().toUpperCase();
        final Person transformedPerson = new Person(firstName, lastName);
        System.out.println("Converting (" + person + ") into ("
                + transformedPerson + ")");
        return transformedPerson;
    }

}

 

最后,運行這個demo的前提是你要把spring batch的數據庫配置好,具體的sql文件在spring batch core jar包中,還有數據源文件,要放在resources目錄中。。

AppMain.java啟動該demo

package com.lyx.batch;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
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 AppMain {

    public static void main(String[] args)
            throws JobExecutionAlreadyRunningException, JobRestartException,
            JobInstanceAlreadyCompleteException, JobParametersInvalidException {
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "classpath:spring-batch2.xml" });
        JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
        Job job = (Job) context.getBean("sampleJob");
        JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
        JobExecution result = launcher.run(job,
                jobParametersBuilder.toJobParameters());
        ExitStatus es = result.getExitStatus();
        if (es.getExitCode().equals(ExitStatus.COMPLETED.getExitCode())) {
            System.out.println("任務正常完成");
        } else {
            System.out.println("任務失敗,exitCode=" + es.getExitCode());
        }
    }
}

如果沒有意外的話,會在數據庫中看到相應的結果。

在該demo的基礎上可以 試驗spring batch的各種特性,可以進一步探索spring batch。

==========END==========

來自:http://my.oschina.net/xinxingegeya/blog/340302

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!