Spring Batch 2.1.8 中文文档(二)

Chapter 2 Spring Batch 新特性

   Spring Batch 2.0正式版特性分为6个主题:

  • Java 5
  • 不连续的分布执行
  • 面向Chunk处理
  • 强化元数据
  • 扩展性
  • 可配置

2.1 JAVA 5

    Spring 1.x都是基于Java 1.4开发的,因此这个系列版本的框架都无法使用Java 5提供的一些增强特性,如泛型,参数化类型等。升级后的框架充分利用了这些特性,但是也无法支持Java 1.4。大部分开发者所打交道的接口都升级为泛型形式。例如ItemReader接口在1.1是如下形式

  1. public interface ItemReader {  
  2.   
  3.     Object read() throws Exception;  
  4.   
  5.     void mark() throws MarkFailedException;  
  6.    
  7.     void reset() throws ResetFailedException;  
  8. }  

这个版本下read方法返回Object类型。到了2.0版本:

  1. public interface ItemReader<T> {  
  2.   
  3.     T read() throws Exception, UnexpectedInputException, ParseException;  
  4.   
  5. }  
     ItemReader现在支持了返回泛型T,也可以看到mark方法和reset方法被去除了,这是由于分布执行的策略改变了,许多其他接口也有类似改变。下文将会逐一讨论。

2.2 面向Chunk处理

    之前Spring Batch默认提供的处理策略是面向Item处理:

在面向item处理中,ItemReader会返回一个对象(即Item)给ItemWriter进行处理,item数量达到提交间隔的要求时提交计算结果。例如,如果提交所要求的item数量为5时,ItemReader和ItemWriter分别会被调用5次。使用简单的代码展示如下:

  1. for(int i = 0; i < commitInterval; i++){  
  2.     Object item = itemReader.read();  
  3.     itemWriter.write(item);  
  4. }  

ItemReader和ItemWriter的接口是完全配合这种解决方案:
  1. public interface ItemReader {  
  2.   
  3.     Object read() throws Exception;  
  4.   
  5.     void mark() throws MarkFailedException;  
  6.    
  7.     void reset() throws ResetFailedException;  
  8. }  

  1. public interface ItemWriter {  
  2.   
  3.     void write(Object item) throws Exception;  
  4.   
  5.     void flush() throws FlushFailedException;  
  6.   
  7.     void clear() throws ClearFailedException;  
  8. }  

由于处理的范围是一个Item,如果要支持回滚场景就需要额外的方法,此时mark,reset,flush和clear就派上了用场。例如,在成功读写了2个item之后,在写第三个item时发生了错误,整个事务就需要回滚,writer中的clear方法会被调用,用于清空缓存,Itemreader中的reset被调用,用于把mark方法所指向的数据游标复原。

在2.0中,这个策略改变为面向chunk方式:

还是上面那个例子,如果提交间隔是5次,那么读操作被调用5次,写操作被调用一次。读Item被汇总到列表中,最终被统一写出,可以简单的表示如下:

  1. List items = new Arraylist();  
  2. for(int i = 0; i < commitInterval; i++){  
  3.     items.add(itemReader.read());  
  4. }  
  5. itemWriter.write(items);  
这个方案不仅更加简单更有扩展性,同时也让ItemReader和ItemWriter接口更加简洁:
  1. public interface ItemReader<T> {  
  2.   
  3.     T read() throws Exception, UnexpectedInputException, ParseException;  
  4.   
  5. }  
  1. public interface ItemWriter<T> {  
  2.   
  3.     void write(List<? extends T> items) throws Exception;  
  4.   
  5. }  

如代码所示,接口不再包含mark,reset,flush和clear方法,这让读和写对象的创建更加直接。ItemReader例子中,接口非常简单,框架会为开发者把读取的item缓存起来,以防rollback情况的发生(针对事务的情况下可能发生例外,查看:Section 5.1.7.1 事务Readers)。ItemWriter也很简单,不再一次一个item的拿取,而是一次把整个item'块'都拿到,在把控制权交还给step前决定资源(如文件或是hibernate的session)的写入。更多面向chunk处理的信息可以在"Section 5.1 面向Chunk处理"中找到。Reader和Writer的实现信息可以查看“Chapter 6 ItemReaders和ItemWriters”。

2.2.1 ItemProcessor

最早,Step只依赖两个对象,ItemReader和ItemWriter:

这个配置是相当健壮的。但是,许多场景下item在写入之前需要转换格式,1.x使用了组合模式


这个模式管用,但是需要在读写与step之间需要额外加入一层。此外,ItemWriter需要和Step单独注册为ItemStream。因此,ItemTransformer被重新命名为ItemProcessor后移到和ItemReader与ItemWriter相同层级:



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 Java configuration and Spring Boot. The Definitive Guide to Spring Batch takes you from the “Hello, World!” of batch processing to complex scenarios demonstrating cloud native techniques for developing batch applications to be run on modern platforms. Finally this book demonstrates how you can use areas of the Spring portfolio beyond just Spring Batch 4 to collaboratively develop mission-critical batch processes. You’ll see how a new class of use cases and platforms has evolved to have an impact on batch-processing. Data science and big data have become prominent in modern IT and the use of batch processing to orchestrate workloads has become commonplace. The Definitive Guide to Spring Batch covers how running finite tasks on cloud infrastructure in a standardized way has changed where batch applications are run. Additionally, you’ll discover how Spring Batch 4 takes advantage of Java 9, Spring Framework 5, and the new Spring Boot 2 micro-framework. After reading this book, you’ll be able to use Spring Boot to simplify the development of your own Spring projects, as well as take advantage of Spring Cloud Task and Spring Cloud Data Flow for added cloud native functionality. Includes a foreword by Dave Syer, Spring Batch project founder. What You’ll Learn Discover what is new in Spring Batch 4 Carry out finite batch processing in the cloud using the Spring Batch project Understand the newest configuration techniques based on Java configuration and Spring Boot using practical examples Master batch processing in complex scenarios including in the cloud Develop batch applications to be run on modern platforms Use areas of the Spring portfolio beyond Spring Batch to develop mission-critical batch processes Who This Book Is For Experienced Java and Spring coders new to the Sp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值