1、项目结构如下:
2、文件说明:
2.1、CreditBill:表示信用卡消费记录领域对象
2.2、CreditBillProcessor:记录处理类,本场景仅打印信息
2.3、credit-card-bill-201910.csv:原始账单数据
2.4、job.xml:作业定义文件
2.5、job-context.xml :Spring Batch 批处理任务需要的基础信息
2.6、JobLaunch:调用批处理作业类
2.7、JobLaunchTest:Junit单元测试类,使用Spring提供的测试框架类。
2.8、pom.xml:引用相关jar包
3、文件内容:
3.1、CreditBill:实体类对象
/***@authormiaosj
*@version1.0
* @date 2019/10/8*/
public classCreditBill {/*** 银行账户*/
privateString accountID;/*** 账户名*/
privateString name;/*** 消费金额*/
private doubleamount;/*** 消费日期*/
privateString date;/*** 消费场所*/
privateString address;publicString getAccountID() {returnaccountID;
}public voidsetAccountID(String accountID) {this.accountID =accountID;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public doublegetAmount() {returnamount;
}public void setAmount(doubleamount) {this.amount =amount;
}publicString getDate() {returndate;
}public voidsetDate(String date) {this.date =date;
}publicString getAddress() {returnaddress;
}public voidsetAddress(String address) {this.address =address;
}
}
3.2、credit-card-bill-201910.csv:原始账单数据
4047390012345678,tom,100.00,2013-2-2 12:00:08,Lu Jia Zui road4047390012345678,tom,320.00,2013-2-3 12:00:08,Lu Jia Zui road4047390012345678,tom,674.00,2013-2-6 12:00:08,Lu Jia road4047390012345678,tom,793.00,2013-2-9 12:00:08,Lu Jia Zui road4047390012345678,tom,360.00,2013-2-11 12:00:08,Lu Jia Zui road4047390012345678,tom,893.00,2013-2-28 12:00:08,Lu Jia Zui road
3.3、job-context.xml:job基础设施
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"default-autowire="byName">
3.4、job.xml:定义job
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">
accountID
name
amount
date
address
3.5、JobLaunch:java调用
importorg.springframework.batch.core.Job;importorg.springframework.batch.core.JobExecution;importorg.springframework.batch.core.JobParameters;importorg.springframework.batch.core.launch.JobLauncher;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;/***@authorE101206
*@version1.0
* @date 2019/10/8*/
public classJobLaunch {
@SuppressWarnings("resource")public static voidmain(String[] args) {//初始化应用上下文
ApplicationContext context = new ClassPathXmlApplicationContext("job/job.xml");//获取作业调度,根据Bean的名称从Spring的上下文获取
JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");//获取任务对象
Job job = (Job) context.getBean("billJob");try{
JobExecution result= launcher.run(job, newJobParameters());
System.out.println(result.toString());
}catch(Exception e) {
e.printStackTrace();
}
}
}
3.6、JobLaunchTest:单位测试
/***@authorE101206
*@version1.0
* @date 2019/10/8*/
importorg.junit.After;importorg.junit.Before;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.batch.core.Job;importorg.springframework.batch.core.JobExecution;importorg.springframework.batch.core.JobParameters;importorg.springframework.batch.core.launch.JobLauncher;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Qualifier;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/job/job.xml"})public classJobLaunchTest {
@AutowiredprivateJobLauncher jobLauncher;
@Autowired@Qualifier("billJob")privateJob job;
@Beforepublic void setUp() throwsException {
}
@Afterpublic void tearDown() throwsException {
}
@Testpublic void billJob() throwsException {
JobExecution result= jobLauncher.run(job, newJobParameters());
System.out.println(result.toString());
}
}
3.7、pom.xml
4.0.0
com.msj
spring-batch-example
1.0-SNAPSHOT
1.8
4.3.8.RELEASE
3.0.7.RELEASE
4.11
org.apache.maven.plugins
maven-compiler-plugin
${jdk.version}
${jdk.version}
org.springframework.batch
spring-batch-core
${spring.batch.version}
org.springframework.batch
spring-batch-test
${spring.batch.version}
junit
junit
${junit.version}
test
4、概念
4.1、Job Repository:作业仓库,负责job、step执行过程中的状态保存
4.2、Job launcher:作业调度器,提供执行job的入口
4.3、Job:作业,由多个step组成,封装整个批处理操作
4.4、Step:作业步,job的一个执行环节
4.5、Tasklet:Step中具体执行逻辑的操作,可以重复执行,可以设置具体的同步、异步操作
4.6、Chunk:给定数量的Item的集合,可以定义对Chunk读操作、处理操作、写操作,提交间隔等
4.7、Item:一条记录
4.8、ItemReader:从数据源读取Item
4.9、ItemProcessor:在Item写入数据源之前,对数据进行处理如:数据清洗,数据转换,数据过滤、数据校验等
4.10、ItemWriter:将Item批量写入数据源