先搞懂几个概念

Job Respository: 作业仓库,负责Job、Step执行过程中的状态保存

Job launcher: 作业调度器,提供执行Job的入口

Job:作业,由多个step组成,封装整个批处理操作

Step:作业步,Job的一个执行环节,由多个或者一个step组装成Job

Tasklet:step中具体执行逻辑的操作,可以重复执行,可以设置具体的同步、异步操作等

Chunk: 给定数量Item的集合,可以定义对Chunk的读操作、处理操作、写操作、提交间隔等

Item:一条数据记录

ItemReader:从数据源中读取Item

ItemProcessor:在Item写入数据源之前,对数据进行处理

ItemWriter:将Item批量写入数据源

实战部分

使用maven创建的工程

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion> <groupId>core-batch</groupId>
<artifactId>core-batch</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>jar</packaging> <name>core-batch</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-infrastructure</artifactId>
<version>3.0..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>3.0..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<version>3.0..RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

定义对象领域

package com.batman.core.batch;

//定义领域对象
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;
} }

定义数据处理类

package com.batman.core.batch;

import org.springframework.batch.item.ItemProcessor;

public class CreditBillProcessor implements ItemProcessor<CreditBill, CreditBill>{

    public CreditBill process(CreditBill bill) throws Exception {
System.out.println(bill.toString());
return bill;
}
}

创建配置文件

<?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>
<!-- 配置任务调度器 用来启动Job-->
<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>

创建job

<?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">
<!-- 定义个billStep的作业步,有一个面向批的操作组成 -->
<step id="billStep">
<!-- 定义批处理操作采用定义的事务管理器,负责批处理中事务管理操作 -->
<tasklet transaction-manager="transactionManager">
<!-- 定义了面向批的操作,定义了读操作csvItemReader,处理操作 creditBillProcessor,写操作csvItemWriter
commit-interval表示提交间隔的大小,即每处理两条数据进行一次写操作-->
<chunk reader="csvItemReader" writer="csvItemWriter"
processor="creditBillProcessor" commit-interval="">
</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"/>
<!-- 通过lineMapper可以把文本中的一行转换成领域对象CreditBill -->
<bean:property name="lineMapper">
<bean:bean
class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<!-- 定义文本中的分割符号 -->
<bean:property name="lineTokenizer" ref="lineTokenizer"/>
<!-- 根据lineTokenizer中定义的names属性映射到creditBill中,最终组装成信用卡账单对象 -->
<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 -->
<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:target/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.batman.core.batch.CreditBill">
</bean:bean>
<!-- 负责处理读入的数据 -->
<bean:bean id="creditBillProcessor" scope="step"
class="com.batman.core.batch.CreditBillProcessor">
</bean:bean>
</bean:beans>

创建main

package com.batman.core.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 {
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 {
//通过JobLauncher的run方法执行billJob任务
JobExecution result = launcher.run(job, new JobParameters());
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
} }

springbatch入门练习(第一篇)的更多相关文章

  1. Entity Framework 6.0 入门系列 第一篇

    Entity Framework 6.0 入门系列 第一篇 好几年前接触过一些ef感觉不是很好用,废弃.但是 Entity Framework 6.0是经过几个版本优化过的产物,性能和功能不断完善,开 ...

  2. VHDL语法入门学习第一篇

    1. 现在先遇到一个VHDL的语法问题,以前没用过VHDL,现在要去研究下,进程(PROCESS) 进程内部经常使用IF,WAIT,CASE或LOOP语句.PROCESS具有敏感信号列表(sensit ...

  3. Flask 入门(第一篇)

    1. 认识 Flask Flask 是一个微型 Web 框架,依赖于 jinjia2 模板系统和 Werkzeug WSGI(本质为 Socket 服务端) 服务,默认情况不支持数据库抽象层.表单验证 ...

  4. LAUNCHXL-28379D入门学习-第一篇

    1. 首先安装controlSUITE或者C2000ware软件,TI官网下载,安装后包括C2000的函数库和例程之类的,还可以和CCS搭配使用.controlSUITE安装完之后大约4个G,所以我安 ...

  5. CC2640 LaunchPad入门试用-第一篇

    1. 先安装固件ble_cc26xx_setupwin32_2_01_00_44423.exe. 2. 打开IAR先找到一个例程测试一下D:\ti\simplelink\ble_cc26xx_2_01 ...

  6. Python 入门demo第一篇

    #-*- coding: UTF-8 -*- 2.7版本对中文的要求 import uuid import socket def get_mac_address(): mac=uuid.UUID(in ...

  7. CodeIgniter 入门教程第一篇:信息发布

    一.MVC CodeIgniter 采用MVC架构即:控制层.模型层和视图层. 对应Application下面的文件夹   (图1): 所有新建文件以.php结尾 视图层 view 文件夹放入HTML ...

  8. Vue入门教程 第一篇 (概念及初始化)

    注:为了本教程的准确性,部分描述引用了官网及网络内容. 安装Vue 1.使用npm安装vue: npm install vue 2.下载使用js文件: https://vuejs.org/js/vue ...

  9. Linux从入门到放弃、零基础入门Linux(第一篇):计算机操作系统简介、linux介绍

    一.计算机操作系统简介 操作系统的定义: 操作系统是一个用来协调.管理和控制计算机硬件和软件资源的系统程序,它位于硬件和应用程序之间. 操作系统的内核的定义: 操作系统的内核是一个管理和控制程序,负责 ...

  10. 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

随机推荐

  1. 解决 iframe 后退不是主页面后退(浏览器 history)问题

    前言:项目中的主页面里有 iframe,切换 iframe 的 src 地址之后,再点浏览器的回退之后,会导致 iframe 里面回退,而不是主页面回退. 问题 浏览器机制的原因,在 iframe 导 ...

  2. SqlSession对象之ParameterHandler

    上一篇讲了StatementHandler,其中有ParameterHandler(参数处理器)是在StatementHandler被创建时被创建的.下面对ParameterHandler进行说明.其 ...

  3. 设计模式(21)--Strategy(策略模式)--行为型

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.模式定义: 策略模式属于对象的行为模式.其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而 ...

  4. BFC(块状格式化上下文)

    今天先来说关于BFC的一些基础知识 BFC是块状格式化上下文,它是一个独立的渲染区域,规定了内部如何布局,并且这个布局和外部毫不相干 触发BFC的方法 1.根元素(即html) 2.float属性不为 ...

  5. 高性能JavaScript(快速响应的用户界面)

    浏览器UI线程 用于执行JavaScript代码和更新界面的进程被称为 “浏览器UI线程” . UI线程的工作基于一个简单的队列系统,任务会被保存到队列中直到线程空闲,一旦空闲队列就被重新提取出来运行 ...

  6. ActiveReports 报表控件V12新特性 -- 新增JSON和CSV导出

    ActiveReports是一款专注于 .NET 平台的报表控件,全面满足 HTML5 / WinForms / ASP.NET / ASP.NET MVC / WPF 等平台下报表设计和开发工作需求 ...

  7. Linux 性能监控之CPU&内存&I/O监控Shell脚本1

    Linux 性能监控之CPU&内存&I/O监控Shell脚本1   by:授客 QQ:1033553122   #!/bin/bash # 获取要监控的本地服务器IP地址 IP=`if ...

  8. Android--播放Gif的取巧办法

    由于做的项目,要有个动画的等待效果,第一时间想到的就是Gif(懒,省事),但是试了好多据说能播放Gif的控件,也写过,但是放到魅族手机上就是不能播放,所有就想了个招,既然Gif能在浏览器上播放,那an ...

  9. Expo大作战(八)--expo中的publish以及expo中的link,对link这块东西没有详细看,大家可以来和我交流

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,将全部来与官网 我猜去全部机翻+个人 ...

  10. 回归JavaScript基础(十)

    主题:创建对象 原型模式 JavaScript中的每个对象都有一个prototype属性(原型属性),这个属性是一个指针,指向一个对象,而这个对象可以由一些属性和方法组成.被指向的对象,可以是多个对象 ...