对第一遍内容的补充

<?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入门练习(第二篇)的更多相关文章

  1. Flask 入门(第二篇)

    1. 数据库 Flask 没有限定使用哪种数据库,不管是 SQL 还是 NoSQL.如果你不想自己编写负责的 SQL语句的话,你也可以使用 ORM,通过 SQLALchemy 即可实现. 1.1 SQ ...

  2. Python 入门demo第二篇

    循环执行逻辑 #-*- coding: UTF-8 -*- import time import urllib2 def task(i): urlstr='http://baidu.com' html ...

  3. Vue入门教程 第二篇 (数据绑定与响应式)

    数据绑定 Vue.js 的核心是一个允许采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统: <div id="app"> {{ message }} </ ...

  4. Linux从入门到放弃、零基础入门Linux(第二篇):在虚拟机vmware中安装linux(一)超详细手把手教你安装centos分步图解

    一.Vmware vmware介绍:VMware,Inc. (Virtual Machine ware)是一个“虚拟PC”软件公司,提供服务器.桌面虚拟化的解决方案.其虚拟化平台的产品包括播放器:它能 ...

  5. RabbitMQ学习总结 第二篇:快速入门HelloWorld

    目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...

  6. 【第二篇】ASP.NET MVC快速入门之数据注解(MVC5+EF6)

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

  7. ElasticSearch入门 第二篇:集群配置

    这是ElasticSearch 2.4 版本系列的第二篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  8. SaltStack 入门到精通第二篇:Salt-master配置文件详解

    SaltStack 入门到精通第二篇:Salt-master配置文件详解     转自(coocla):http://blog.coocla.org/301.html 原本想要重新翻译salt-mas ...

  9. Egret入门学习日记 --- 第二篇 (书籍的选择 && 书籍目录 && 书中 3.3 节 内容)

    第二篇 (书籍的选择 && 书籍目录 && 书中 3.3 节 内容) 既然选好了Egret,那我就要想想怎么学了. 开始第一步,先加个Q群先,这不,拿到了一本<E ...

  10. 学会Git玩转GitHub(第二篇) 入门详解 - 精简归纳

    学会Git玩转GitHub(第二篇) 入门详解 - 精简归纳 JERRY_Z. ~ 2020 / 10 / 25 转载请注明出处!️ 目录 学会Git玩转GitHub(第二篇) 入门详解 - 精简归纳 ...

随机推荐

  1. Java Servlet 过滤器与 springmvc 拦截器的区别?

    前言:在工作中,遇到需要记录日志的情况,不知道该选择过滤器还是拦截器,故总结了一下. servlet 过滤器 定义 java过滤器能够对目标资源的请求和响应进行截取.过滤器的工作方式分为四种 应用场景 ...

  2. 设计模式之建造者模式(Buider)(5)

    简介 在软件开发中,也会存在一些构造非常复杂的对象,这些对象拥有一系列的成员属性,这些成员属性有些是基本数据类型,有些是引用类型,总之就是一句话,这个对象的构建比较复杂.在这里我们就将复杂对象当做汽车 ...

  3. Codeforces834A

    A. The Useless Toy time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  4. python学习之老男孩python全栈第九期_day005作业

    1,有如下变量(tu是个元组),请实现要求的功能. tu = ("alex", [11, 22, {"k1": 'v1', "k2": [& ...

  5. python的深浅拷贝以及fromkeys的用法

    1.join()的用法:使用前面的字符串.对后面的列表进行拼接,拼接结果是一个字符串 # lst = ["alex","dsb",'wusir','xsb'] ...

  6. 【代码笔记】iOS-将字符串中特定后的字变成红色

    一,效果图. 二,代码. ViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup ...

  7. ionic3 下创建ionic1项目

    一 start命令 ionic start sdscapp --type=ionic1 ——添加平台命令 ionic cordova platform add android

  8. The D Programming Language 书评

    此书的作者 Andrei Alexandrescu 作为前 C++ 社区的一朵奇葩,因为实在是不满 C++ 标准委员会的官僚作风,跳槽到了 D 社区,成为了 D 发明人 Walt Brightman ...

  9. atitit.交换机 汇聚上联、网络克隆和标准共享的原理与区别

    atitit.交换机 汇聚上联.网络克隆和标准共享的原理与区别 1. 标准共享(标准化模式)1 2. 汇聚上联trunk1 2.1. 使用场合1 2.2. 背景1 2.3. 实现原理2 3. 网络克隆 ...

  10. JUnit手动设计测试方法以及与Randoop的自动生成测试的比较

    手动设计测试 在已有的web project本地目录lib文件夹里导入两个jar文件(版本可不一样):junit-4.12.jar和hamcrest.jar 打开eclipse,导入项目,右击项目选择 ...