Yana
Yana

Reputation: 313

Spring Batch read step running in loop

I came across a piece of code that reads some data as the following:

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

However when I run the job this read is running in a loop. I found from another stackoverflow answer that it is the expected behavior. My question then is what is the best solution given this particular example? Extend StudioReader from JdbcCursorItemReader ? I found one example that defines everything in the xml which I don't want. And here is the context.xml part for the reader:

  <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>

And here is the job definition in 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>

Writer:

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

The read method after suggestion from @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();
}

Upvotes: 4

Views: 5036

Answers (1)

holi-java
holi-java

Reputation: 30686

spring-batch documentation for ItemReader.read assert:

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

But your read method is always return a List and should be like this:

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

if you want your read method return a List then you must paging the results like this:

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

if you can't paging the results from Service you can solved like this:

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

it's better not read a list of items List<Studio>, read an item at a time Studio instead. when you read a list of item you possibly duplicated iterate logic between writers and processors as you have shown the demo in comments. if you have a huge of data list to processing you can combine pagination in your reader, for example:

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

Maybe you need to see step processing mechanism.

Upvotes: 2

Related Questions