1. 程式人生 > >從頭認識SpringBatch批處理框架--例項場景一信用卡消費對賬

從頭認識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: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">
	<!--引入job-context.xml配置檔案-->
    <bean:import resource="classpath:job-context.xml"/>
    <!--定義billJob billStep 包含讀資料  處理資料 寫資料-->
    <job id="billJob">
        <step id="billStep">
            <tasklet transaction-manager="transactionManager">
            	<!--commit-interval="2" 表示任務提交間隔的大小 此處表示每處理2條資料 進行一次寫入操作-->
                <chunk reader="csvItemReader" writer="csvItemWriter" 
                    processor="creditBillProcessor" commit-interval="2">
                </chunk>
            </tasklet>
        </step>
    </job>
    <!-- 讀取信用卡賬單檔案,CSV格式 -->
    <bean:bean id="csvItemReader"
        class="org.springframework.batch.item.file.FlatFileItemReader" 
        scope="step">
    	<!--設定讀取的檔案資源-->
        <bean:property name="resource" 
            value="classpath:credit-card-bill-201303.csv"/>
         <!--將文字中的每行記錄轉換為領域物件-->
        <bean:property name="lineMapper">
            <bean:bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
               <!--引用lineTokenizer-->
               <bean:property name="lineTokenizer" ref="lineTokenizer"/>
                <!--fieldSetMapper根據lineTokenizer中定義的names屬性對映到領域物件中去-->
                <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 定義文字中每行的分隔符號 以及每行對映成FieldSet物件後的name列表 -->
    <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: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.my.domain.CreditBill">
    </bean:bean>
    <!--負責業務資料的處理-->
    <bean:bean id="creditBillProcessor" scope="step"
        class="com.my.processor.CreditBillProcessor">
    </bean:bean>
</bean:beans>
5.建立ItemProcessor
該類主要是用於處理ItemReader讀取的資料,通常包括資料過濾、資料轉換等操作,此處我們只是簡單的列印賬單資訊。具體程式碼如下: