spring-boot – Spring循环读取步骤

我遇到了一段代码,它读取了一些数据,如下所示:

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建议后的read方法:

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();
}

最佳答案 ItemReader.read断言的弹簧批文档:

Implementations must return null at the end of the input data set.

但是你的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;
}

最好不要读取列表< Studio>项目列表,而是在Studio时刻读取项目.当您阅读项目列表时,您可能会重复在编写器和处理器之间迭代逻辑,因为您已在注释中显示演示.如果您有大量的数据列表要处理,您可以在阅读器中组合分页,例如:

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.

> ItemReader – 一次阅读一个项目.
> ItemProcessor – 一次处理一个项目.
> ItemWriter – 写出entire chunk of items.

点赞