Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot Spring批量读取步骤在循环中运行_Spring Boot_Java 8_Spring Batch - Fatal编程技术网

Spring boot Spring批量读取步骤在循环中运行

Spring boot Spring批量读取步骤在循环中运行,spring-boot,java-8,spring-batch,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(); @Overr

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

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;
   }
公共类StudioReader实现ItemReader{
@塞特私人区道区道;
@Getter@Setter私有批处理上下文;
私有HopsService HopsService=新HopsService();
@凌驾
public List read()引发异常{
List List=hopsService.getStudioHops();
如果(!isEmpty(列表)){
用于(工作室:列表){
log.info(“正在读取的工作室:{}”,Studio.getCode());
List areaList=areaDao.getArea(studio
.getCode());
如果(areaList.size()>0){
setArea((字符串)areaList.get(0));
log.info(“为studio{2}获取区域{1}”)、areaList.get(0)、studio.getCode();
}
this.getContext().setReadCount(1);
}
}
退货清单;
}
但是,当我运行作业时,该读取是在循环中运行的。我从另一个stackoverflow中发现这是预期的行为。然后我的问题是,给出这个特定示例的最佳解决方案是什么?从JdbcCursorItemReader扩展StudioReader?我发现一个示例定义了xml中我不需要的所有内容。下面是c面向读者的ontext.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);
   }
公共无效写入(列表0){
setArea((字符串)areaList.get(0));
log.info(“为studio{2}获取区域{1}”)、areaList.get(0)、studio.getCode();
}
this.getContext().setReadCount(1);
}
返回Collections.singletonList(listOfStudiosFromApi.iterator();
}

断言的spring批处理文档:

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

但是您的read方法总是返回一个列表,应该是这样的:

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;
}
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;
}
也许你需要看看

  • -一次读取一个项目
  • -一次处理一个项目
  • -写出来

可能是@MichaelMinella的复制品他从字面上链接到了那篇文章,更不用说那篇文章甚至没有解决方案…@Yana
foreach
循环
初始化
方法应该使用
项目处理器
ItemReader
ItemReader
违反不属于责任范围。谢谢-但有点困惑,因为你的示例没有返回列表-你能澄清一下吗?@Yana一个读者一次读一个项目,而不是一个项目列表。如果你读一个项目列表,那么你的作者可能会这样:
write(list)
@Yana如果您一次读取项目列表,则必须在
阅读器中分页结果。read
@Yana最好不要一次读取项目列表,这将导致
阅读器
编写器
处理器
中出现重复。read()方法就是我在这里看到的——我也看到了这一点——但是我不确定它里面到底是什么
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;
}
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;
}