Spring Batch学习笔记——steps之间共享数据

时间:2015-04-03 01:38:00   收藏:0   阅读:1562

名词说明:

上下文:

执行:

执行上下文:

案例:

警告:一旦steps共享数据,这些数据就会把这些steps连接起来。努力使steps独立。如果你实在是不能独立他们,才使用下面的技术。你应该把数据共享作为steps不能独立的后备方案。

1 数据共享方式:

2 使用execution context来共享数据

什么是execution context:ExecutionContext类就代表了执行上下文,它就是一个适合与批应用的 键值对的一个 map。下面就是一个从上下文读写数据的例子:

executionContext.putString("importId", importId);
String importId = jobExecutionContext.getString("importId");

an execution context只是a job execution的一部分,并且不同的执行上下文

Jobs 和 steps 有它们自己的execution context

    Spring Batch提供了两种execution context: job execution context和step execution context。它们都是ExecutionContext类型但是它们的作用域不同。下图说明了在一个job execution过程中的两种执行上下文。

技术分享

那么怎么访问执行上下文呢?你需要引用对应的执行:JobExecution:如果你想访问job执行上下文,StepExecution:如果你想访问step执行上下文。几乎所有的Spring Batch artifacts能够很容易的访问JobExecution和StepExecution,不幸的是item reader,processor,writer功能不能访问它们。不过你可以实现一个监听接口(如:ItemStream)来洞察execution.下面列举了ItemReader通过实现ItemStream接口来访问执行上下文

清单1:

public class FilesInDirectoryItemReader implements 
       ItemReader
 
  
 
  
   
  
   
  , ItemStream {

 
  
 
  
@Override
public void open(ExecutionContext executionContext)
    throws ItemStreamException { }
@Override
public void update(ExecutionContext executionContext)
    throws ItemStreamException { }
@Override
public void close() throws ItemStreamException { }
@Override
public File read() throws Exception, UnexpectedInputException,
    ParseException, NonTransientResourceException { (...) }
}
用job execution context在steps之间共享数据
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!