Spring Batch读取步骤在循环中运行

时间:2017-04-06 04:29:46

标签: spring-boot java-8 spring-batch

我遇到了一段代码,它读取了以下数据:

public class StudioReader implements ItemReader<List<Studio>> {
   @Setter private AreaDao areaDao;
   @Getter @Setter private BatchContext context;
   private HopsService hopsService = new HopsService();

   @Override
   public List<Studio> read() throws Exception {
      List<Studio> list = hopsService.getStudioHops();
      if (!isEmpty(list)) {
         for (Studio studio : list) {
            log.info("Studio being read: {}", studio.getCode());
            List areaList = areaDao.getArea(studio
                  .getCode());
            if (areaList.size() > 0) {
               studio.setArea((String) areaList.get(0));
               log.info("Area {1} is fetched for studio {2}", areaList.get(0), studio.getCode());
            }
            this.getContext().setReadCount(1);
         }
      }
      return list;
   }

然而,当我运行该作业时,此读取正在循环运行。我从另一个stackoverflow answer发现它是预期的行为。那么我的问题是,在这个特定的例子中,最佳解决方案是什么?从JdbcCursorItemReader扩展StudioReader?我找到了一个例子,它定义了我不想要的xml中的所有内容。这是读者的context.xml部分:

  <bean class="org.springframework.batch.core.scope.StepScope" />
   <bean id="ItemReader" class="com.syc.studio.reader.StudioReader" scope="step">
      <property name="context" ref="BatchContext" />
      <property name="areaDao" ref="AreaDao" />
   </bean>

这是xml中的作业定义:

 <bean id="StudioJob" class="org.springframework.batch.core.job.SimpleJob">
      <property name="steps">
         <list>
                     <bean id="StudioStep" parent="SimpleStep" >
                     <property name="itemReader" ref="ItemReader"/>
                     <property name="itemWriter" ref="ItemWriter"/>
                     <property name="retryableExceptionClasses">
                        <map>
                           <entry key="com.syc.studio.exception.CustomException" value="true"/>
                        </map>
                     </property>
                     <property name="retryLimit" value="2" />
                     </bean>
         </list>
      </property>
      <property name="jobRepository" ref="jobRepository" />
   </bean>

编剧:

public void write(List<? extends Object> obj) throws Exception {
   List<Studio> list = (List<Studio>) obj.get(0);
   for (int i = 0; i <= list.size(); i++) {
      Studio studio = list.get(i);
      if (apiClient == null) {
        apiClient = new APIClient("v2");
     }
      this.uploadXML(studio);
   }

@ holi-java建议后的读取方法:

public List<Studio> read() throws Exception {
    if (this.listIterator == null) {
        this.listIterator = initializing();
    }
    return this.listIterator.hasNext() ? this.listIterator.next() : null;
}

private Iterator<List<Studio>> initializing() {
    List<Studio> listOfStudiosFromApi = hopsService.getStudioLocations();
    for (Studio studio : listOfStudiosFromApi) {
        log.info("Studio being read: {}", studio.getCode());
        List areaList = areaDao.getArea(studio.getCode());
        if (areaList.size() > 0) {
            studio.setArea((String) areaList.get(0));
            log.info("Area {1} is fetched for studio {2}", areaList.get(0), studio.getCode());
        }
        this.getContext().setReadCount(1);
    }
    return Collections.singletonList(listOfStudiosFromApi).iterator();
}

1 个答案:

答案 0 :(得分:2)

ItemReader.read断言的弹出批处理文档:

  

实现必须在输入数据集的末尾返回 null

但是你的read方法总是返回一个List,应该是这样的:

public Studio read() throws Exception {
    if (this.results == null) {
        List<Studio> list = hopsService.getStudioHops();
        ...
        this.results=list.iterator();
    }
    return this.results.hasNext() ? this.results.next() : null;
}

如果你希望你的read方法返回一个List,那么你必须像这样分页结果:

public List<Studio> read() throws Exception {
    List<Studio> results=hopsService.getStudioHops(this.page++);
    ...
    return results.isEmpty()?null:results;
}

如果您无法从服务中分页结果,您可以这样解决:

public List<Studio> read() throws Exception {
    if(this.results==null){
     this.results = Collections.singletonList(hopsService.getStudioHops()).iterator();
    }

    return this.results.hasNext()?this.results.next():null;
}

最好不要阅读项目列表List<Studio>,而是一次读取一个项目Studio。当您阅读可能重复的项目列表时,在writersprocessors之间迭代逻辑,因为您已在评论中显示了演示。如果您有大量的数据列表要处理,您可以在阅读器中组合分页,例如:

public Studio read() throws Exception {
    if (this.results == null || !this.results.hasNext()) {
        List<Studio> list = hopsService.getStudioHops(this.page++);
        ...
        this.results=list.iterator();
    }

    return this.results.hasNext() ? this.results.next() : null;
}

也许您需要查看step processing mechanism