場景說明html
我的使用信用卡消費,銀行按期發送銀行卡消費帳單,本例將模擬銀行處理我的信用卡消費對帳單對帳,銀行須要按期地把我的消費的記錄導出成csv文件,而後交給對帳系統處理。java
主要流程:spring
(從credit-card-bill-201303.csv)讀取數據---->處理數據----->寫數據到 outputFile文件數據庫
項目結構app

項目結構說明:框架
- CreditBill:信用卡消費記錄領域對象
- CreditBillProcessor:信用卡消費記錄處理類
- jobLaunch:調用批處理做業類
- jobLaunchTest:Junit單元測試,使用Spring提供的測試框架
- credit-card-bill-201303.csv:信用卡消費帳單文件
- job-context.xml:定義做業基礎信息
- job.xml:定義做業文件
- outputFile.xml:輸出處理事後的信用卡消費帳單文件
項目實現步驟詳解:
1.準備credit-card-bill-201303.csv對帳文件
這裏咱們使用csv格式的文件,該文件的每一行表示信用卡消費記錄,中間用逗號分隔,分別表示:
銀行卡帳戶、帳戶名、消費金額、消費日期、消費場所以下圖所示:
2.定義領域對象:
爲了與帳單文件造成映射,須要新建信用卡消費記錄對象 -CreditBill,主要屬性:銀行卡帳戶、帳戶名、消費金額、消費日期、消費場所
具體代碼以下:
- package com.my.domain;
- public class CreditBill {
-
- private String accountID;
-
- private String name;
-
- private double amount;
-
- private String date;
-
- private String address;
-
- public String getAccountID() {
- return accountID;
- }
-
- public void setAccountID(String accountID) {
- this.accountID = accountID;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public double getAmount() {
- return amount;
- }
-
- public void setAmount(double amount) {
- this.amount = amount;
- }
-
- public String getDate() {
- return date;
- }
-
- public void setDate(String date) {
- this.date = date;
- }
-
- public String getAddress() {
- return address;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
-
-
- }
3.定義job-context.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: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"
- default-autowire="byName">
-
- <bean id="jobRepository"
- class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
- </bean>
-
- <bean id="jobLauncher"
- class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
- <property name="jobRepository" ref="jobRepository"/>
- </bean>
-
- <bean id="transactionManager"
- class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
- </beans>
4.定義job.xml文件
job.xml文件主要配置批處理做業Job、Step、ItemReader(讀數據)、ItemProcessoe(處理數據)、 ItemWriter(寫數據) 具體的配置以下圖:
- <?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-context.xml"/>
-
- <job id="billJob">
- <step id="billStep">
- <tasklet transaction-manager="transactionManager">
-
- <chunk reader="csvItemReader" writer="csvItemWriter"
- processor="creditBillProcessor" commit-interval="2">
- </chunk>
- </tasklet>
- </step>
- </job>
-
- <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">
-
- <bean:property name="lineTokenizer" ref="lineTokenizer"/>
-
- <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>
-
- <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>
-
-
- <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讀取的數據,一般包括數據過濾、數據轉換等操做,此處咱們只是簡單的打印帳單信息。具體代碼以下:
- package com.my.processor;
-
- import org.springframework.batch.item.ItemProcessor;
-
- import com.my.domain.CreditBill;
-
- public class CreditBillProcessor implements ItemProcessor<CreditBill, CreditBill> {
-
- public CreditBill process(CreditBill bill) throws Exception {
- System.out.println("信用卡消費領域對象:"+bill.getAccountID()+"-"+bill.getName()+"-"+bill.getAmount()+"-"+bill.getAddress());
- return bill;
- }
- }
6.啓動Job
咱們這裏介紹兩種啓動運行方式,
第一種:main方法
- package com.my.batch;
-
- import org.springframework.batch.core.Job;
- import org.springframework.batch.core.JobExecution;
- import org.springframework.batch.core.JobParameters;
- import org.springframework.batch.core.launch.JobLauncher;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class JobLaunch {
-
-
- @SuppressWarnings("resource")
- 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 {
- JobExecution result = launcher.run(job, new JobParameters());
- System.out.println(result.toString());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
第二種:Junit單元測試
- package com.my.batch;
-
-
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.batch.core.Job;
- import org.springframework.batch.core.JobExecution;
- import org.springframework.batch.core.JobParameters;
- import org.springframework.batch.core.launch.JobLauncher;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
-
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"job.xml"})
- public class JobLaunchTest {
- @Autowired
- private JobLauncher jobLauncher;
-
- @Autowired@Qualifier("billJob")
- private Job job;
-
- @Before
- public void setUp() throws Exception {
- }
-
- @After
- public void tearDown() throws Exception {
- }
-
- @Test
- public void billJob() throws Exception {
- JobExecution result = jobLauncher.run(job, new JobParameters());
- System.out.println(result.toString());
- }
- }
執行Job後 outputFile文件已錄入數據
控制檯信息:
這裏只顯示了兩條信用卡消費帳單信息,由於咱們在job.xml文件中設置任務提交間隔的大小 每處理2條數據 進行一次寫入操做。以下圖
總結:
從這個實例場景中,咱們學會了使用SpringBatch已經提供好的功能組件和基礎設施,快速搭建批處理應用。瞭解了SpringBatch的一些基本概念:
JobRepository:做業倉庫,負責Job、Step執行過程當中的狀態保存;
JobLauncher:做業調度器,提供執行Job的入口;
Job:做業,由一個或多個的Step組成;
Step:做業步,Job的一個執行環節,由一個或多個的Step組成Job
Tasklet:Stp中具體執行邏輯的操做,能夠重複執行,能夠設置具體的同步、異步操做
Chunk:給定數量的Item集合
Item:一條數據記錄
ItenReader:從數據源(文件、數據庫或消息隊列等)中獲取Item
ItemProcessor:在Item寫數據以前,對數據進行數據過濾、數據清洗、數據轉換、數據校驗等操做
ItemWriter:將Item數據記錄批量寫入數據源(文件、數據庫或消息隊列等)