场景说明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数据记录批量写入数据源(文件、数据库或消息队列等)