springbatch入门练习(第二篇)

对第一遍内容的补充

<?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:task="http://www.springframework.org/schema/task" 
    xmlns:context="http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/batch 
    http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    
    <!-- 作业仓库 -->
    <job-repository id="jobRepository" data-source="dataSource"
        transaction-manager="transactionManager" isolation-level-for-create="SERIALIZABLE"
        table-prefix="BATCH_" max-varchar-length="1000"
    />
    
    <!-- 作业调度器 -->
    <bean:bean id="jobLauncher" 
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <bean:property name="jobRepository" ref="jobRepository"/>
    </bean:bean>
    <!-- 配置大小为1的线程池 -->
    <task:executor id="executor" pool-size="1" />
    <!-- 异步作业调度器 -->
    <bean:bean id="jobLauncherAsyn" 
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <bean:property name="jobRepository" ref="jobRepository"/>
        <!-- 为作业调度器配置执行的线程池,作业执行期间会从线程池中选择一个线程执行job -->
        <bean:property name="taskExecutor" ref="executor" />
    </bean:bean>
    <!-- 事务管理器 -->
    <bean:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <bean:property name="dataSource" ref="dataSource" />
    </bean:bean>
    
    <!-- 数据源 -->
    <bean:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <bean:property name="driverClassName">
               <bean:value>com.mysql.jdbc.Driver</bean:value>
          </bean:property>
          <bean:property name="url">
               <bean:value>jdbc:mysql://127.0.0.1:3306/springbatch</bean:value>
          </bean:property>
          <bean:property name="username" value="root"></bean:property>
          <bean:property name="password" value="123456"></bean:property>
     </bean:bean>
</bean:beans>
<?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-3.0.xsd  
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/batch 
    http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">
    <bean:import resource="classpath:job-context03.xml"/>
    <!-- 通过abstract属性定义baseJob为抽象的job,抽象的job不能被实例化 -->
    <job id="baseJob" abstract="true">
        <listeners>
            <listener ref="sysoutListener"></listener>
        </listeners>
    </job>
    <!-- billjob继承 baseJob,billjob拥有父类的所有特性,billjob执行的时候也会调用basejob中
                 定义的拦截器sysoutListener -->
    <!-- 定义一个批处理作业   restartable默认值是true支持重新启动,false不支持重新启动-->
    <job id="billJob" parent="baseJob" restartable="true">
        <!-- 定义个billStep的作业步,有一个面向批的操作组成 -->
        <step id="billStep">
            <!-- 定义批处理操作采用定义的事务管理器,负责批处理中事务管理操作 -->
            <tasklet transaction-manager="transactionManager">
                <!-- 定义了面向批的操作,定义了读操作csvItemReader,处理操作 creditBillProcessor,写操作csvItemWriter
                 commit-interval表示提交间隔的大小,即每处理两条数据进行一次写操作-->
                <chunk reader="csvItemReader" writer="csvItemWriter" 
                    processor="creditBillProcessor" commit-interval="2">
                </chunk>
            </tasklet>
        </step>
        <!-- 定义拦截器 -->
        <!-- 如果父子中均定义了拦截器,通过设置merge属性为true对拦截器进行合并,如果为false,
                                                            则子类中的拦截器覆盖掉父类中的拦截器 -->
        <listeners merge="true">     
            <listener ref="systemOutJobExecutionListener"></listener>
          <!--   <listener ref="sysoutListener"></listener>
            <listener ref="systemOutJobExecutionListener"></listener> -->
        </listeners>
        <!-- 定义参数校验 -->
        <validator ref="validator"></validator>
    </job>
    <!-- 执行该job的时候,必须输入date参数,最多输入date、name两个参数,任何其他参数的名字都会导致校验类不通过 -->
    <bean:bean id="validator"
        class="org.springframework.batch.core.job.DefaultJobParametersValidator">
        <bean:property name="requiredKeys">
            <bean:set>
                <bean:value>date</bean:value>
            </bean:set>
        </bean:property>
        <bean:property name="optionalKeys">
            <bean:set>
                <bean:value>inputResource</bean:value>
            </bean:set>
        </bean:property>
    </bean:bean>
    <bean:bean id="sysoutListener" class="com.batman.core.batch.listener.SystemOut">
    </bean:bean>
    <bean:bean id="systemOutJobExecutionListener" class="com.batman.core.batch.listener.SystemOutJobExecutionListener">
    </bean:bean>
    <!-- 读取信用卡账单文件,CSV格式 -->
    <!-- 通过设置scope="step"来定义 csvItemReader的生命周期与step绑定
                                通过使用step scope,可以设置属性的late binding(属性后绑定)能力-->
    <bean:bean id="csvItemReader"
        class="org.springframework.batch.item.file.FlatFileItemReader" 
        scope="step">
        <!-- 读取文件资源 -->
        <bean:property name="resource" 
            value="#{jobParameters['inputResource']}"/>
             <!--  value="classpath:credit-card-bill-201303.csv"/> -->
        <!-- 通过lineMapper可以把文本中的一行转换成领域对象CreditBill -->
        <bean:property name="lineMapper">
            <bean:bean 
                class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <!-- 定义文本中的分割符号 -->
                <bean:property name="lineTokenizer" ref="lineTokenizer"/>
                <!-- 根据lineTokenizer中定义的names属性映射到creditBill中,最终组装成信用卡账单对象 -->
                <bean:property name="fieldSetMapper">
                    <bean:bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <bean:property name="prototypeBeanName" value="creditBill">
                        </bean:property>
                    </bean:bean>
                </bean:property>
            </bean:bean>
        </bean:property>
    </bean:bean>
    <!-- lineTokenizer -->
    <bean:bean id="lineTokenizer" 
        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
        <bean:property name="delimiter" value=","/>
        <bean:property name="names">
            <bean:list>
                <bean:value>accountID</bean:value>
                <bean:value>name</bean:value>
                <bean:value>amount</bean:value>
                <bean:value>date</bean:value>
                <bean:value>address</bean:value>
            </bean:list>
        </bean:property>
    </bean:bean>
    
    <!-- 写信用卡账单文件,CSV格式 -->
    <bean:bean id="csvItemWriter" 
        class="org.springframework.batch.item.file.FlatFileItemWriter" 
        scope="step">
        <bean:property name="resource" value="file:target/outputFile.csv"/>
        <bean:property name="lineAggregator">
            <bean:bean 
                class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                <bean:property name="delimiter" value=","></bean:property>
                <bean:property name="fieldExtractor">
                    <bean:bean 
                        class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                        <bean:property name="names" 
                             value="accountID,name,amount,date,address">
                        </bean:property>
                    </bean:bean>
                </bean:property>
            </bean:bean>
        </bean:property>
    </bean:bean>
    
    <bean:bean id="creditBill" scope="prototype"
        class="com.batman.core.batch.CreditBill">
    </bean:bean>
    <!-- 负责处理读入的数据 -->
    <bean:bean id="creditBillProcessor" scope="step"
        class="com.batman.core.batch.CreditBillProcessor">
    </bean:bean>
</bean:beans>
package com.batman.core.batch;

import java.util.Date;

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 {
    /**
     * 执行批处理作业.<br>
     * @param jobPath    作业配置文件
     * @param jobName    作业名
     * @param builder    作业参数构造器
     */
    public static void executeJob(String jobPath, String jobName, JobParametersBuilder builder) {
        ApplicationContext context = new ClassPathXmlApplicationContext(jobPath);
        JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
        Job job = (Job) context.getBean(jobName);
        try {
            JobExecution result = launcher.run(job, builder.toJobParameters());
            System.out.println(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //
    public static void executeJobAsyn(String jobPath, String jobName, JobParametersBuilder builder) {
        ApplicationContext context = new ClassPathXmlApplicationContext(jobPath);
        JobLauncher launcher = (JobLauncher) context.getBean("jobLauncherAsyn");
        Job job = (Job) context.getBean(jobName);
        try {
            JobExecution result = launcher.run(job, builder.toJobParameters());
            System.out.println(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        /*executeJob("job.xml", "billJob",
                new JobParametersBuilder().addString("date", "20130308"));*/
        /*executeJob("job.xml", "billJob",
                new JobParametersBuilder().addDate("date", new Date())
                .addString("name", "aad")
                .addString("inputResource", "classpath:credit-card-bill-201303.csv"));*/
        /*-------异步执行--------*/
        executeJobAsyn("job.xml", "billJob",
                new JobParametersBuilder().addDate("date", new Date())
                .addString("inputResource", "classpath:credit-card-bill-201303.csv"));
    }
    /*public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "job.xml");
        //获取作业调度器
        JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
        //获取任务对象
        Job job = (Job) context.getBean("billJob");
        try {
            //通过JobLauncher的run方法执行billJob任务
            JobExecution result = launcher.run(job, new JobParameters());
            System.out.println(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

}

猜你喜欢

转载自www.cnblogs.com/mutong1228/p/9053849.html