springbatch入门练习(第二篇)
对第一遍内容的补充
<?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:task="http://www.springframework.org/schema/task"
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
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- 作业仓库 -->
<job-repository id="jobRepository" data-source="dataSource"
transaction-manager="transactionManager" isolation-level-for-create="SERIALIZABLE"
table-prefix="BATCH_" max-varchar-length=""
/> <!-- 作业调度器 -->
<bean:bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<bean:property name="jobRepository" ref="jobRepository"/>
</bean:bean>
<!-- 配置大小为1的线程池 -->
<task:executor id="executor" pool-size="" />
<!-- 异步作业调度器 -->
<bean:bean id="jobLauncherAsyn"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<bean:property name="jobRepository" ref="jobRepository"/>
<!-- 为作业调度器配置执行的线程池,作业执行期间会从线程池中选择一个线程执行job -->
<bean:property name="taskExecutor" ref="executor" />
</bean:bean>
<!-- 事务管理器 -->
<bean:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<bean:property name="dataSource" ref="dataSource" />
</bean:bean> <!-- 数据源 -->
<bean:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<bean:property name="driverClassName">
<bean:value>com.mysql.jdbc.Driver</bean:value>
</bean:property>
<bean:property name="url">
<bean:value>jdbc:mysql://127.0.0.1:3306/springbatch</bean:value>
</bean:property>
<bean:property name="username" value="root"></bean:property>
<bean:property name="password" value=""></bean:property>
</bean:bean>
</bean:beans>
<?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-context03.xml"/>
<!-- 通过abstract属性定义baseJob为抽象的job,抽象的job不能被实例化 -->
<job id="baseJob" abstract="true">
<listeners>
<listener ref="sysoutListener"></listener>
</listeners>
</job>
<!-- billjob继承 baseJob,billjob拥有父类的所有特性,billjob执行的时候也会调用basejob中
定义的拦截器sysoutListener -->
<!-- 定义一个批处理作业 restartable默认值是true支持重新启动,false不支持重新启动-->
<job id="billJob" parent="baseJob" restartable="true">
<!-- 定义个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>
<!-- 定义拦截器 -->
<!-- 如果父子中均定义了拦截器,通过设置merge属性为true对拦截器进行合并,如果为false,
则子类中的拦截器覆盖掉父类中的拦截器 -->
<listeners merge="true">
<listener ref="systemOutJobExecutionListener"></listener>
<!-- <listener ref="sysoutListener"></listener>
<listener ref="systemOutJobExecutionListener"></listener> -->
</listeners>
<!-- 定义参数校验 -->
<validator ref="validator"></validator>
</job>
<!-- 执行该job的时候,必须输入date参数,最多输入date、name两个参数,任何其他参数的名字都会导致校验类不通过 -->
<bean:bean id="validator"
class="org.springframework.batch.core.job.DefaultJobParametersValidator">
<bean:property name="requiredKeys">
<bean:set>
<bean:value>date</bean:value>
</bean:set>
</bean:property>
<bean:property name="optionalKeys">
<bean:set>
<bean:value>inputResource</bean:value>
</bean:set>
</bean:property>
</bean:bean>
<bean:bean id="sysoutListener" class="com.batman.core.batch.listener.SystemOut">
</bean:bean>
<bean:bean id="systemOutJobExecutionListener" class="com.batman.core.batch.listener.SystemOutJobExecutionListener">
</bean:bean>
<!-- 读取信用卡账单文件,CSV格式 -->
<!-- 通过设置scope="step"来定义 csvItemReader的生命周期与step绑定
通过使用step scope,可以设置属性的late binding(属性后绑定)能力-->
<bean:bean id="csvItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
<!-- 读取文件资源 -->
<bean:property name="resource"
value="#{jobParameters['inputResource']}"/>
<!-- 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>
package com.batman.core.batch; import java.util.Date; import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class JobLaunch {
/**
* 执行批处理作业.<br>
* @param jobPath 作业配置文件
* @param jobName 作业名
* @param builder 作业参数构造器
*/
public static void executeJob(String jobPath, String jobName, JobParametersBuilder builder) {
ApplicationContext context = new ClassPathXmlApplicationContext(jobPath);
JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean(jobName);
try {
JobExecution result = launcher.run(job, builder.toJobParameters());
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
//
public static void executeJobAsyn(String jobPath, String jobName, JobParametersBuilder builder) {
ApplicationContext context = new ClassPathXmlApplicationContext(jobPath);
JobLauncher launcher = (JobLauncher) context.getBean("jobLauncherAsyn");
Job job = (Job) context.getBean(jobName);
try {
JobExecution result = launcher.run(job, builder.toJobParameters());
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
/*executeJob("job.xml", "billJob",
new JobParametersBuilder().addString("date", "20130308"));*/
/*executeJob("job.xml", "billJob",
new JobParametersBuilder().addDate("date", new Date())
.addString("name", "aad")
.addString("inputResource", "classpath:credit-card-bill-201303.csv"));*/
/*-------异步执行--------*/
executeJobAsyn("job.xml", "billJob",
new JobParametersBuilder().addDate("date", new Date())
.addString("inputResource", "classpath:credit-card-bill-201303.csv"));
}
/*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入门练习(第二篇)的更多相关文章
- Flask 入门(第二篇)
1. 数据库 Flask 没有限定使用哪种数据库,不管是 SQL 还是 NoSQL.如果你不想自己编写负责的 SQL语句的话,你也可以使用 ORM,通过 SQLALchemy 即可实现. 1.1 SQ ...
- Python 入门demo第二篇
循环执行逻辑 #-*- coding: UTF-8 -*- import time import urllib2 def task(i): urlstr='http://baidu.com' html ...
- Vue入门教程 第二篇 (数据绑定与响应式)
数据绑定 Vue.js 的核心是一个允许采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统: <div id="app"> {{ message }} </ ...
- Linux从入门到放弃、零基础入门Linux(第二篇):在虚拟机vmware中安装linux(一)超详细手把手教你安装centos分步图解
一.Vmware vmware介绍:VMware,Inc. (Virtual Machine ware)是一个“虚拟PC”软件公司,提供服务器.桌面虚拟化的解决方案.其虚拟化平台的产品包括播放器:它能 ...
- RabbitMQ学习总结 第二篇:快速入门HelloWorld
目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...
- 【第二篇】ASP.NET MVC快速入门之数据注解(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- ElasticSearch入门 第二篇:集群配置
这是ElasticSearch 2.4 版本系列的第二篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- SaltStack 入门到精通第二篇:Salt-master配置文件详解
SaltStack 入门到精通第二篇:Salt-master配置文件详解 转自(coocla):http://blog.coocla.org/301.html 原本想要重新翻译salt-mas ...
- Egret入门学习日记 --- 第二篇 (书籍的选择 && 书籍目录 && 书中 3.3 节 内容)
第二篇 (书籍的选择 && 书籍目录 && 书中 3.3 节 内容) 既然选好了Egret,那我就要想想怎么学了. 开始第一步,先加个Q群先,这不,拿到了一本<E ...
- 学会Git玩转GitHub(第二篇) 入门详解 - 精简归纳
学会Git玩转GitHub(第二篇) 入门详解 - 精简归纳 JERRY_Z. ~ 2020 / 10 / 25 转载请注明出处!️ 目录 学会Git玩转GitHub(第二篇) 入门详解 - 精简归纳 ...
随机推荐
- Asp.Net里关于Session过期跳转页面的一些小技巧
这里算是自己的个人随笔吧,仅供参考使用,后续有更好的方法再做补充 之前在Aspx页面里面,在Session过期的时候我经常会使用 Server.Transfer("b.aspx") ...
- HDU1814(2-SAT)
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- python-建造者模式
源码地址:https://github.com/weilanhanf/PythonDesignPatterns 说明: 假如要组装一台电脑,将主板,CPU,内存等部件按照某个稳定的步骤组合,基本过程是 ...
- Tornado入门
一.概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像是一个py文件,不过为了能有效利用非阻塞式服务器环境,这 ...
- Flex布局的学习经验
做为css布局的又一种新方式,Flex拥有极强的使用效果,相比原来的float,position对元素样式的操作更加简洁,本文是我的一点学习经验和心得吧,如有错误以及不足之处,请多多指点. 好进入正题 ...
- [基础知识]row类visible使用
使用row的visibe属性,要反向遍历rowset,因为如果正向遍历,rowset是实时变化的,行号是错误的.正确代码如下: Local integer &k; For &k = & ...
- java持有对象【1】容器类及ArrayList
如果一个程序只包含固定数量的且其生命周期都是已知的对象,那么这是一个非常简单的程序. ----------java编程思想第十一章引言 java有许多方式引用对象,例如学过的数组,他是编译器支持的类 ...
- Debian 常用命令
换源 用中科大的比较快 deb http://mirrors.ustc.edu.cn/debian jessie main contrib non-free deb-src http://mirror ...
- oracle 11.2.0.1 rman异机恢复 11.2.0.3(windows X64)
问题原因: 误操作,需要时间点恢复. 备份情况:rman 备份,每天一次全备份,并且附带备份当天所有产生的archivelog,无expdp备份 恢复目标: 恢复到9号晚上21点数据 源系统:WIND ...
- SQLSERVER 分区表实战
背景:对NEWISS数据库创建分区表T_SALES的SQL.按照日期来进行分区步骤:1:创建文件组2:创建数据文件3:创建分区函数4:创建分区方案5:创建表及聚集索引6:导入测试数据(此处略),并查询 ...