栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring Batch Job从多个来源读取

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Batch Job从多个来源读取

没有可立即使用的组件可以执行您的要求;唯一的解决方案是编写一个

ItemReader<>
委托给
JdbcCursorItemReader
(或委托给或委托
HibernateCursorItemReader
给任何通用
ItemReader
实现)的自定义。
您需要准备所有必要的东西(数据源,会话,实际数据库读取器),并将所有委派的读取器绑定到您的自定义读取器。

编辑:您需要使用recussion来模拟一个循环,

ItemReader.read()
并在整个作业重新启动期间保留阅读器和委托状态。

class MyItemReader<T> implements ItemReader<T>, ItemStream {  private ItemReader[] delegates;  private int delegateIndex;  private ItemReader<T> currentDelegate;  private ExecutionContext stepExecutionContext;  public void setDelegates(ItemReader[] delegates) {    this.delegates = delegates;  }  @BeforeStep  private void beforeStep(StepExecution stepExecution) {    this.stepExecutionContext = stepExecution.getExecutionContext();  }  public T read() {    T item = null;    if(null != currentDelegate) {      item = currentDelegate.read();      if(null == item) {        ((ItemStream)this.currentDelegate).close();        this.currentDelegate = null;      }    }    // Move to next delegate if previous was exhausted!    if(null == item && this.delegateIndex< this.delegates.length) {      this.currentDelegate = this.delegates[this.currentIndex++];      ((ItemStream)this.currentDelegate).open(this.stepExecutionContext);      update(this.stepExecutionContext);      // Recurse to read() to simulate loop through delegates      item = read();    }    return item;  }  public void open(ExecutionContext ctx) {    // During open restore last active reader and restore its state    if(ctx.containsKey("index")) {      this.delegateIndex = ctx.getInt("index");      this.currentDelegate = this.delegates[this.delegateIndex];      ((ItemStream)this.currentDelegate ).open(ctx);    }  }  public void update(ExecutionContext ctx) {    // Update current delegate index and state    ctx.putInt("index", this.delegateIndex);    if(null != this.currentDelegate) {      ((ItemStream)this.currentDelegate).update(ctx);    }  }  public void close(ExecutionContext ctx) {    if(null != this.currentDelegate) {      ((ItemStream)this.currentDelegate).close();  }}

<bean id="myItemReader" class=path.to.MyItemReader>  <property name="delegates">    <array>      <ref bean="itemReader1"/>      <ref bean="itemReader2"/>      <ref bean="itemReader3"/>    </array>  </property></bean>

EDIT2:记住设置属性 名称 ;这是让MyItemReader.read()正常工作所必需的

<bean id="itemReader1" >  <property name="name" value="itemReader1" />  <!-- Set other properties --></bean>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/486123.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号