Skip to content

Instantly share code, notes, and snippets.

@theflofly
Created January 26, 2016 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theflofly/1070206b40e25a599e80 to your computer and use it in GitHub Desktop.
Save theflofly/1070206b40e25a599e80 to your computer and use it in GitHub Desktop.
package com.tcb.issue1.job;
import com.tcb.issue1.model.Car;
import com.tcb.issue1.repository.CarRepository;
import org.springframework.batch.item.data.AbstractPaginatedDataItemReader;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Iterator;
import static org.springframework.util.ClassUtils.getShortName;
/**
* This class is the item reader for the car object. It reads cars from the elasticsearch database
* and returns them. The doPageRead method is called several times until it returns null.
*
* Created by Florian.Courtial on 11/01/16.
*/
@Component
public class CarItemReader extends AbstractPaginatedDataItemReader<Car> implements InitializingBean {
private Pageable carPageable;
@Autowired
private CarRepository carRepository;
@PostConstruct
public void init() {
setName(getShortName(getClass()));
// by default 100 cars are returned on each call
this.carPageable = new PageRequest(0,100);
}
@Override
protected Iterator<Car> doPageRead() {
Page<Car> cars = carRepository.findAll(this.carPageable);
carPageable = carPageable.next();
return cars.iterator();
}
@Override
public void afterPropertiesSet() throws Exception {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment