- 浏览: 161077 次
- 性别:
- 来自: 武汉
文章分类
- 全部博客 (95)
- oracle (7)
- java 基础 (10)
- web层 (3)
- 框架技术 (0)
- eclipse技巧 (2)
- 重构 (2)
- 架构 (1)
- 缓存 (11)
- 分布式多线程编程 (5)
- jquery (8)
- ibatis (3)
- spring API (1)
- maven (1)
- spring (4)
- js (12)
- 正则表达式 (1)
- Velocit (1)
- 杂七杂八 (3)
- XSS跨域攻击 (1)
- appfuse (2)
- Simple-Spring-Memcached (3)
- spring batch (11)
- spring integration (2)
- Teiid 8.0 (0)
- Linux 命令 (1)
最新评论
-
luxianping:
怎么根据命名空间来删除缓存? 比如说用户对应的菜单,当有用户 ...
使用SSM注解做缓存操作 -
vemacitri_b:
mowengaobo 写道你可以这样理解key。为什么不单独放 ...
使用SSM注解做缓存操作 -
mowengaobo:
你可以这样理解key。为什么不单独放个参数出来如 @Rea ...
使用SSM注解做缓存操作 -
夜神月:
目前我在dao曾有如下的注解配置:@ReadThroughSi ...
使用SSM注解做缓存操作
Spring Batch 之 Sample(XML文件操作)(五)
前篇关于Spring Batch的文章,讲述了Spring Batch 对CSV文件的读写操作。 本文将通过一个完整的实例,与大家一起讨论运用Spring Batch对XML文件的读写操作。实例流程是从一个XML文件中读取商品信息,经过简单的处理,写入另外一个XML文件中。
工程结构如下图:
log4j.xml是log处理的配置文件,与本文没有必然联系,再此不做论述。
application.xml文件内容如下:
<?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"> <!-- auto scan path --> <context:component-scan base-package="com.wanggc.springbatch.sample.xml" /> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <property name="jobRepository" ref="jobRepository" /> </bean> <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" /> <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"> </bean> </beans>
17行是base-spckage的指定,是spring的用法。
19-22行配置的jobLauncher用来启动Job。
24行配置的jobRepository为job提供持久化操作。
26-28行的transactionManager提供事物管理操作。
本文核心配置文件batch.xml内容如下:
<?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" xmlns:util="http://www.springframework.org/schema/util" 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.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean:import resource="applicationContext.xml" /> <job id="xmlFileReadAndWriterJob"> <step id="xmlFileReadAndWriterStep"> <tasklet> <chunk reader="xmlReader" writer="xmlWriter" processor="xmlProcessor" commit-interval="10"> </chunk> </tasklet> </step> </job> <!-- XML文件读取 --> <bean:bean id="xmlReader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="step"> <bean:property name="fragmentRootElementName" value="goods" /> <bean:property name="unmarshaller" ref="tradeMarshaller" /> <bean:property name="resource" value="file:#{jobParameters['inputFilePath']}"></bean:property> </bean:bean> <!-- XML文件写入 --> <bean:bean id="xmlWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter" scope="step"> <bean:property name="rootTagName" value="wanggc" /> <bean:property name="marshaller" ref="tradeMarshaller" /> <bean:property name="resource" value="file:#{jobParameters['outputFilePath']}" /> </bean:bean> <bean:bean id="tradeMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"> <bean:property name="aliases"> <util:map id="aliases"> <bean:entry key="goods" value="com.wanggc.springbatch.sample.xml.pojo.Goods" /> <bean:entry key="buyDay" value="java.util.Date"></bean:entry> </util:map> </bean:property> </bean:bean> </bean:beans>
21-29行配置了Job的基本信息。此Job包含一个Step,Step中包含了基本的读(xmlReader),处理(xmlProcessor),写(xmlWriter)。
32-38行配置了对XML文件读操作。对XML的读是由SpringBatch提供的StaxEventItemReader类来完成。要读取一个XML文件,首先要知道这个文件的存放路径,resource属性就是指定文件路径信息的。知道了文件路径,还需要知道要读取的XML的根节点名称,fragmentRootElementName属性就是指定根节点名称的。知道了根节点名称,还需要知道的一点就是怎么解析这个节点信息,unmarshaller就负责完成解析节点信息,并映射成程序pojo对象。注意,根节点并不是指整个XML文件的根节点,而是指要读取的信息片段的根节点,不管这个节点片段处在哪一层,框架都会遍历到。
49-58行配置了解析XML节点信息的unmarshaller。其中entry的key指定对应根节点名称goods,value指定程序的pojo类,这样,程序就可以将goods节点下的子节点与pojo类(Goods)中的属性去匹配,当匹配到子节点名与pojo类中的属性名相同时,就会将子节点的内容赋值给pojo类的属性。这样就完成了一个根节点的读取,框架会控制循环操作,直到将文件中所有根(goods)节点全部读完为止。这样就完成了XML文件的读操作。
41-47行配置了对XML文件的写操作。与读XML文件一样,要写一个XML文件,也是需要知道这个文件的文件的存放路径的,同样是resource属性提供文件的路径信息。同时,也是需要知道这个文件的跟节点信息的,rootTagName属性提供根节点名信息。注意此处的根节点,指整个文件的跟节点,与读得时候稍有区别,从两个属性的名称上也可以看出。有了上面的信息,完成一个写操作,还需要一个把pojo对象转换成XML片段的工具,由marshaller提供。本文读操作的unmarshaller和写操作的marshaller用的是同一个转换器,因为XStreamMarshaller既提供将节点片段转换为pojo对象功能,同时又提供将pojo对象持久化为xml文件的功能。如果写的内容与读得内容有很大差异,可以另外配置一个转换器。
batch.xml文件配置的对XML文件的读写操作,至于读出来的信息做怎么样的处理再写入文件,通过简单的配置恐怕就无法完成了,就需要我们自己写代码完成了。XMLProcessor类就是完成这个工作的。只要在Job的配置文件中指定到这个类就可以了。XMLProcessor类的内容如下:
package com.wanggc.springbatch.sample.xml; import java.util.Date; import org.springframework.batch.item.ItemProcessor; import org.springframework.stereotype.Component; import com.wanggc.springbatch.sample.xml.pojo.Goods; /** * XML文件处理类。 */ @Component("xmlProcessor") public class XMLProcessor implements ItemProcessor<Goods, Goods> { /** * XML文件内容处理。 * */ @Override public Goods process(Goods goods) throws Exception { // 购入日期变更 goods.setBuyDay(new Date()); // 顾客信息变更 goods.setCustomer(goods.getCustomer() + "顾客!"); // ISIN变更 goods.setIsin(goods.getIsin() + "IsIn"); // 价格变更 goods.setPrice(goods.getPrice() + 1000.112); // 数量变更 goods.setQuantity(goods.getQuantity() + 100); // 处理后的数据返回 return goods; } }
内容很简单,再此就不做赘述了。要注意的一点就是红背景色的地方。加了此标签无须在batch.xml文件增加对xmlProcessor声明用的bean,可以在job中直接引用,这是Spring的功能。当然,实现这个的前提是要在applicationContext.xml中配置base-package,只有这样才能找到。
工程结构图中的XMLLaunch类用来启动Job。内容如下:
package com.wanggc.springbatch.sample.xml;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
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 XMLLaunch {
/**
* @param args
*/
public static void main(String[] args1) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"batch.xml");
JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("xmlFileReadAndWriterJob");
try {
// JOB实行
JobExecution result = launcher.run(job, new JobParametersBuilder()
.addString("inputFilePath", "C:\\input.xml")
.addString("outputFilePath", "C:\\output.xml")
.toJobParameters());
// 运行结果输出
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意其中为Job提供的两个动态参数,以及在配置文件中的用法。
pojo类Goods的内容如下:
package com.wanggc.springbatch.sample.xml.pojo;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 商品信息类.
*/
public class Goods {
/** isin号 */
private String isin;
/** 数量 */
private int quantity;
/** 价格 */
private double price;
/** 客户 */
private String customer;
/** 购入日期 */
private Date buyDay;
/* getter he setter已经删除 */
}
input.xml文件内容如下:
处理结果如下(output.xml):
下次,将和大家一起讨论关于Spring Batch 对固定长内容文件的读写问题。
发表评论
-
Spring Batch(一)
2012-05-15 17:38 1371最近因为工作需要抽空学习了一下Spring Batch ... -
Spring Batch 之 JobParameters (十)
2012-05-14 16:30 2616继续前面关于Spring Batch系列的文章,本文 ... -
Spring Batch 之 skip讲解(九)
2012-05-14 16:29 1633前面的文章跟大家一起讨论了Spring Batch的概念, ... -
Spring Batch 之 Sample(游标方式读写DB数据表)(八)
2012-05-14 16:26 1750前面关于Spring Batch的文章,讲述了 ... -
Spring Batch 之 Sample(复合格式文件的读、多文件的写)(七)
2012-05-14 16:24 2462前面关于Spring Batch的文章,讲述了Spri ... -
Spring Batch 之 Sample(固定长格式文件读写)(六)
2012-05-14 16:23 2540前篇关于Spring Batch的文章,讲 ... -
Spring Batch 之 Sample(CSV文件操作)(四)
2012-05-10 15:10 1685Spring Batch 之 Sample(C ... -
Spring Batch 之 Sample(Hello World)(三)
2012-05-10 15:07 1133通过前面两篇关于Spring Batch文 ... -
Spring Batch 之 框架流程简单介绍(二)
2012-05-10 15:06 1152Spring Batch流程介绍: 上图描绘了Spr ... -
Spring Batch 之 Spring Batch 简介(一)
2012-05-10 15:05 1322Spring Batch 之 Spring Batch 简 ...
相关推荐
主要给大家介绍了Spring Batch读取txt文件并写入数据库的方法,SpringBatch 是一个轻量级、全面的批处理框架。这里我们用它来实现文件的读取并将读取的结果作处理,处理之后再写入数据库中的功能。需要的朋友可以...
spring-batchr的分区示例的简单实现。
Spring Batch是Spring的一个子项目,使用Java语言并基于Spring框架为基础开发,使得已经使用 Spring 框架的开发者或者企业更容易访问和利用企业服务。 Spring Batch 提供了大量可重用的组件,包括了日志、追踪、事务、...
SpringBatch Tasklet sample
Spring Batch API(Spring Batch 开发文档).CHM。 官网 Spring Batch API,Spring Batch 开发文档
Spring Boot整合Spring Batch的一个小例子,在网上发现这方面的资源比较少,特此将其上传供大家学习。
spring-batch4.0.0 batch spring-batch集成 spring-batch.jar
以及Spring Batch框架中经典的三步走策略:数据读、数据处理和数据写,详尽地介绍了如何对CVS格式文件、JSON格式文件、XML文件、数据库和JMS消息队列中的数据进行读操作、处理和写操作,对于数据库的操作详细介绍了...
Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统。Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问和利用企业级服务。Spring Batch可以提供...
spring batch批处理框架和对应的源码资源 rar 可以直接运行的
Work with all aspects of batch processing in a modern Java environment using a selection of Spring frameworks. This book provides up-to-date examples using the latest configuration techniques based on...
四,Spring Batch之Step执行过程介绍 6 五,Spring Batch应用 7 1,简单应用 7 构建应用 7 对象定义 7 读写及处理接口 8 任务定义 10 任务执行 11 任务重试 13 运行时管理 14 2,高级应用 16 ...
springboot2.0中使用java配置来实现springbatch,摒弃之前的xml文件配置。
基于Spring Batch的大数据量并行处理 基于Spring Batch的大数据量并行处理
最近在研究springBoot+springbatch ,按照官网的实例做了一个实例。 最近在研究springBoot+springbatch ,按照官网的实例做了一个实例。
Spring Batch in Action is a comprehensive, in-depth guide to writing batch applications using Spring Batch. Written for developers who have basic knowledge of Java and the Spring lightweight ...
常见的几种SpringBatch运行的成功案例,本人亲测,绝对可靠!你值得拥有
难得的详细spring batch资料 难得的详细spring batch资料
mybatis、springBatch、mysql、quartz、spring、springMVC 部署说明: 本项目为两个数据库,由一个数据库的表向另外一个数据库的表做数据迁移,其中数据库脚本在:/src/main/resources/sql/下面(其中data_rep中的表...
Spring Batch批处理框架Spring Batch批处理框架Spring Batch批处理框架