java jdbc批处理框架_SpringBatch批处理框架:入门项目

1、项目结构如下:

X8vfOsxG+IBCAAAAABJRU5ErkJggg==

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批量写入数据源

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值