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") ...
- java.lang.ExceptionInInitializerError异常
今天在开发的过程中,遇到java.lang.ExceptionInInitializerError异常,百度查了一下,顺便学习学习,做个笔记 静态初始化程序中发生意外异常的信号,抛出Exception ...
- springboot 初识
从实用主义来学习springboot的话,那我们期望的就是首先知道 1 他是个什么东西 2 我们为什么要用他,他能带来什么样的好处 3 如何快速上手 简单来讲,springboot你可以理解成spri ...
- Javascript经典算法学习1:产生随机数组的辅助类
辅助类 在几个经典排序算法学习部分,为方便统一测试不同算法,新建了一个辅助类,主要功能为:产生指定长度的随机数组,提供打印输出数组,交换两个元素等功能,代码如下: function ArraySort ...
- svn本地连接服务器失败,但是浏览器可以
tortoise svn无法连接到服务器,清空“Autherticate data”后,再进行更新,提交,log查看等操作,svn还是不提示输入用户名和密码,而是报: error: Unable to ...
- 纯小白入手 vue3.0 CLI - 2.2 - 组件 home.vue 的初步改造
vue3.0 CLI 真小白一步一步入手全教程系列:https://www.cnblogs.com/ndos/category/1295752.html 我的 github 地址 - vue3.0St ...
- 火狐浏览器sqlite插件
https://addons.mozilla.org/zh-cn/firefox/addon/sqlite-manager/
- html垂直居中
参考于http://www.cnblogs.com/yugege/p/5246652.html <!DOCTYPE html> <html lang="en"&g ...
- JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架)
JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架) 本来想实现 QQ 登录,有域名一直没用过,还得备案,好麻烦,只能过几天再更新啦. 先把实现的发送邮箱验证码更能更新了. 老规矩,更多内容在注释 ...
- TreeView失去焦点时亮显选中状态
Windows Form下设置属性即可. TreeView.HideSelection = false